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

wingify / vwo-php-sdk / 6946821539

21 Nov 2023 04:31PM UTC coverage: 92.326% (-0.1%) from 92.439%
6946821539

push

github

rohitesh-wingify
fix(MEG): Issues with Advanced implementation of MEG

4 of 4 new or added lines in 1 file covered. (100.0%)

14 existing lines in 1 file now uncovered.

1937 of 2098 relevant lines covered (92.33%)

91.09 hits per line

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

32.79
/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;
462✔
36

37
        return $this;
462✔
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) {
102✔
80
            return false;
102✔
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
        if(isset($params[VisitorConstants::USER_AGENT]))
×
109
            $request .= VisitorConstants::CUSTOM_HEADER_USER_AGENT . ': ' . $params[VisitorConstants::USER_AGENT] . "\r\n";
×
110
        if(isset($params[VisitorConstants::IP]))
×
UNCOV
111
            $request .= VisitorConstants::CUSTOM_HEADER_IP . ': ' . $params[VisitorConstants::IP] . "\r\n";
×
UNCOV
112
        $request .= 'Connection: Close' . "\r\n\r\n";
×
113

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

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

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

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

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

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

168
        if (isset($response['httpStatus']) && $response['httpStatus'] == 200) {
×
169
            LoggerService::log(
×
UNCOV
170
                Logger::INFO,
×
171
                'IMPRESSION_BATCH_SUCCESS',
×
172
                [
UNCOV
173
                    '{endPoint}' => $url,
×
UNCOV
174
                    '{accountId}' => $params['a']
×
175
                ],
176
                self::CLASSNAME
177
            );
178
            return true;
×
179
        }
UNCOV
180
        LoggerService::log(Logger::ERROR, 'IMPRESSION_FAILED', ['{endPoint}' => $url, '{err}' => ''], self::CLASSNAME);
×
UNCOV
181
        return false;
×
182
    }
183
}
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

© 2026 Coveralls, Inc