• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wingify / vwo-php-sdk / 5829543375

pending completion
5829543375

push

github

rohitesh-wingify
feat(ipadd_useragent): add user ip address and browser user agent

33 of 33 new or added lines in 3 files covered. (100.0%)

1917 of 2070 relevant lines covered (92.61%)

84.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.9
/src/Utils/EventDispatcher.php
1
<?php
2

3
/**
4
 * Copyright 2019-2022 Wingify Software Pvt. Ltd.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *    http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18

19
namespace vwo\Utils;
20

21
use Monolog\Logger as Logger;
22
use vwo\Constants\FileNameEnum;
23
use vwo\Constants\Urls;
24
use vwo\Services\LoggerService;
25
use vwo\HttpHandler\Connection as Connection;
26
use vwo\Constants\Visitor as VisitorConstants;
27

28
class EventDispatcher
29
{
30
    static $isDevelopmentMode;
31
    const CLASSNAME = FileNameEnum::EVENT_DISPATCHER;
32

33
    function __construct($isDevelopmentMode)
34
    {
35
        self::$isDevelopmentMode = $isDevelopmentMode;
426✔
36

37
        return $this;
426✔
38
    }
39

40
    /**
41
     * API to track data
42
     *
43
     * @param string url
44
     * @param array      $parameters
45
     *
46
     * @return boolean
47
     */
48
    public function send($url, $parameters)
49
    {
50

51
        if (self::$isDevelopmentMode) {
12✔
52
            return false;
×
53
        } else {
54
            $connection = new Connection();
12✔
55

56
            $response = $connection->get($url, $parameters);
12✔
57
        }
58

59
        if (isset($response['httpStatus']) && $response['httpStatus'] == 200) {
12✔
60
            return $response;
×
61
        }
62
        LoggerService::log(Logger::ERROR, 'IMPRESSION_FAILED', ['{endPoint}' => $url, '{err}' => ''], self::CLASSNAME);
12✔
63

64
        return false;
12✔
65
    }
66

67
    /**
68
     * Send async call to the destination i.e. VWO server
69
     *
70
     * @param string $url
71
     * @param string $method
72
     * @param array  $params
73
     *
74
     * @return int|false
75
     */
76
    public function sendAsyncRequest($url, $method, $params = [])
77
    {
78
        // If in DEV mode, do not send any call
79
        if (self::$isDevelopmentMode) {
90✔
80
            return false;
90✔
81
        }
82

83
        // Parse url and extract information
84
        $parsedUrl = parse_url($url);
×
85
        $host = $parsedUrl['host'];
×
86
        $path = $parsedUrl['path'];
×
87

88
        // Open socket connectio
89
        $socketConnection = fsockopen('ssl://' . $host, 443, $errno, $errstr, 60);
×
90
        if (!$socketConnection) {
×
91
            LoggerService::log(
×
92
                Logger::ERROR,
×
93
                'IMPRESSION_FAILED',
×
94
                [
95
                    '{endPoint}' => $url,
×
96
                    '{err}' => 'Unable to connect to ' . $host . '. Error: ' . $errstr . ' ' . ($errno)
×
97
                ],
98
                self::CLASSNAME
99
            );
100

101
            return false;
×
102
        }
103

104
        // Build request
105
        $request  = $method . ' ' . $path . '?' . http_build_query($params);
×
106
        $request .= ' HTTP/1.1' . "\r\n";
×
107
        $request .= 'Host: ' . $host . "\r\n";
×
108
        $request .= VisitorConstants::CUSTOM_HEADER_USER_AGENT . ': ' . $params[VisitorConstants::USER_AGENT] . "\r\n";
×
109
        $request .= VisitorConstants::CUSTOM_HEADER_IP . ': ' . $params[VisitorConstants::IP] . "\r\n";
×
110
        $request .= 'Connection: Close' . "\r\n\r\n";
×
111

112
        // Send Request
113
        $result = fwrite($socketConnection, $request);
×
114
        fclose($socketConnection);
×
115
        return $result;
×
116
    }
117

118
    /**
119
     * Send call to the destination i.e. VWO server
120
     *
121
     * @param  array $params
122
     * @param  array $postData
123
     * @return bool
124
     */
125
    public function sendEventRequest($params = [], $postData = [])
126
    {
127
        if (self::$isDevelopmentMode) {
48✔
128
            return false;
24✔
129
        } else {
130
            $connection = new Connection();
24✔
131

132
            $url = Common::getEventsUrl() . '?' . http_build_query($params);
24✔
133
            $connection->addHeader('User-Agent', ImpressionBuilder::SDK_LANGUAGE);
24✔
134
            $connection->addHeader(VisitorConstants::CUSTOM_HEADER_USER_AGENT, $params[VisitorConstants::USER_AGENT]);
24✔
135
            $connection->addHeader(VisitorConstants::CUSTOM_HEADER_IP,  $params[VisitorConstants::IP]);
24✔
136
            $response = $connection->post($url, $postData);
24✔
137
        }
138

139
        if (isset($response['httpStatus']) && $response['httpStatus'] == 200) {
24✔
140
            return true;
24✔
141
        }
142
        LoggerService::log(Logger::ERROR, 'IMPRESSION_FAILED', ['{endPoint}' => $url, '{err}' => ''], self::CLASSNAME);
×
143
        return false;
×
144
    }
145

146
    /**
147
     * Send batch events call to the destination i.e. VWO server
148
     *
149
     * @param  string $sdkKey
150
     * @param  array $params
151
     * @param  array $postData
152
     * @return bool
153
     */
154
    public function sendBatchEventRequest($sdkKey, $params, $postData)
155
    {
156
        if (self::$isDevelopmentMode) {
×
157
            return false;
×
158
        } else {
159
            $connection = new Connection();
×
160

161
            $url = Common::getBatchEventsUrl() . '?' . http_build_query($params);
×
162
            $connection->addHeader('Authorization', $sdkKey);
×
163
            $response = $connection->post($url, $postData);
×
164
        }
165

166
        if (isset($response['httpStatus']) && $response['httpStatus'] == 200) {
×
167
            LoggerService::log(
×
168
                Logger::INFO,
×
169
                'IMPRESSION_BATCH_SUCCESS',
×
170
                [
171
                    '{endPoint}' => $url,
×
172
                    '{accountId}' => $params['a']
×
173
                ],
174
                self::CLASSNAME
175
            );
176
            return true;
×
177
        }
178
        LoggerService::log(Logger::ERROR, 'IMPRESSION_FAILED', ['{endPoint}' => $url, '{err}' => ''], self::CLASSNAME);
×
179
        return false;
×
180
    }
181
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc