GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#9)
by
unknown
02:23
created

Event::addUserProperties()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace Zumba\Amplitude;
3
4
/**
5
 * Event object, used to make a consistent interface and serialization of the event JSON used in Amplitude API calls
6
 *
7
 * To maintain better parity with the official Amplitude HTTP API, can set the built in properties using underscored
8
 * name (e.g. user_id instead of userId), though camelcase is recommended for better looking code.
9
 *
10
 * @property string $userId
11
 * @property string $deviceId
12
 * @property string $eventType
13
 * @property int $time
14
 * @property array $eventProperties Any values set that do not match built-in property will be set in this array
15
 * @property array $userProperties
16
 * @property string $appVersion
17
 * @property string $platform
18
 * @property string $osName
19
 * @property string $osVersion
20
 * @property string $deviceBrand
21
 * @property string $deviceManufacturer
22
 * @property string $deviceModel
23
 * @property string $deviceType
24
 * @property string $carrier
25
 * @property string $country
26
 * @property string $region
27
 * @property string $city
28
 * @property string $dma
29
 * @property string $language
30
 * @property float $price
31
 * @property int $quantity
32
 * @property float $revenue
33
 * @property string $productId
34
 * @property string $revenueType
35
 * @property float $locationLat
36
 * @property float $locationLng
37
 * @property string $ip
38
 * @property string $idfa
39
 * @property string $adid
40
 */
41
class Event implements \JsonSerializable
42
{
43
    /**
44
     * Array of data for this event
45
     *
46
     * @var array
47
     */
48
    protected $data = [];
49
50
    /**
51
     * Array of built-in properties used for events, and the data type for each one.
52
     *
53
     * The name used here is what is expected by the Amplitude HTTP API, and how the data will be stored internally,
54
     * however these can be set/retrieved using camelcase.
55
     *
56
     * @var array
57
     */
58
    protected $availableVars = [
59
        'user_id' => 'string',
60
        'device_id' => 'string',
61
        'event_type' => 'string',
62
        'time' => 'int',
63
        'event_properties' => 'array',
64
        'user_properties' => 'array',
65
        'app_version' => 'string',
66
        'platform' => 'string',
67
        'os_name' => 'string',
68
        'os_version' => 'string',
69
        'device_brand' => 'string',
70
        'device_manufacturer' => 'string',
71
        'device_model' => 'string',
72
        'device_type' => 'string',
73
        'carrier' => 'string',
74
        'country' => 'string',
75
        'region' => 'string',
76
        'city' => 'string',
77
        'dma' => 'string',
78
        'language' => 'string',
79
        'price' => 'float',
80
        'quantity' => 'int',
81
        'revenue' => 'float',
82
        'productId' => 'string',
83
        'revenueType' => 'string',
84
        'location_lat' => 'float',
85
        'location_lng' => 'float',
86
        'ip' => 'string',
87
        'idfa' => 'string',
88
        'adid' => 'string',
89
    ];
90
91
    /**
92
     * Constructor
93
     *
94
     * @param array $data Initial data to set on the event
95
     */
96
    public function __construct(array $data = [])
97
    {
98
        if (!empty($data)) {
99
            $this->set($data);
100
        }
101
    }
102
103
    /**
104
     * Add user properties
105
     *
106
     * If called multiple times, the first time a user property is set will take precedence.
107
     *
108
     * If need to overwrite a property already set, you can manipulate $event->userProperties array directly
109
     *
110
     * @param array $userProperties
111
     * @return \Zumba\Amplitude\Event
112
     */
113
    public function addUserProperties(array $userProperties)
114
    {
115
        $props = !empty($this->userProperties) ? $this->userProperties : [];
116
        $this->userProperties = $props + $userProperties;
117
        return $this;
118
    }
119
120
    /**
121
     * Set a value in the event.
122
     *
123
     * If the name matches one of the built-in event properties, such as user_id, device_id, etc. OR matches the camel
124
     * case version like userId, deviceId etc. - it will set the built-in property, casting the value to the
125
     * appropriate type for that property
126
     *
127
     * If the name does not match either underscore or camcelcase version of a built in event property name, it will
128
     * set the value in the event_properties array.
129
     *
130
     * It also accepts an array of key => value pairs for the first argument, to pass in an array of properties to set.
131
     *
132
     * All of these are equivelent, and any of these would set the event property "device_brand" to "HTC":
133
     *
134
     * <code>
135
     * $event->set('device_brand', 'HTC');
136
     * $event->set('deviceBrand', 'HTC');
137
     * // Object magic methods
138
     * $event->device_brand = 'HTC';
139
     * $event->deviceBrand = 'HTC';
140
     * // setting array
141
     * $event->set(['device_brand' => 'HTC']);
142
     * $event->set(['deviceBrand' => 'HTC']);
143
     * </code>
144
     *
145
     * All of the above are equivelent, use whatever is most appropriate for your project / situation.
146
     *
147
     * Note that only built-in event properties are normalized to match the built-in name.  Custom properties that get
148
     * set in event_properties are not normalized.  Meaning if you use a camelcase name, name with spaces in it, etc,
149
     * it will use that name as-is without attempting to normalize.
150
     *
151
     * @param string|array $name If array, will set key:value pairs
152
     * @param string $value Not used if first argument is an array
153
     * @return \Zumba\Amplitude\Event
154
     */
155
    public function set($name, $value = null)
156
    {
157
        if (is_array($name)) {
158
            foreach ($name as $key => $val) {
159
                $this->set($key, $val);
160
            }
161
            return $this;
162
        }
163
        $name = $this->normalize($name);
164
        if (!isset($this->availableVars[$name])) {
165
            // treat it like an event_property
166
            $this->data['event_properties'][$name] = $value;
167
            return $this;
168
        }
169
170
        switch ($this->availableVars[$name]) {
171
            case 'string':
172
                $value = (string)$value;
173
                break;
174
            case 'int':
175
                $value = (int)$value;
176
                break;
177
            case 'float':
178
                $value = (float)$value;
179
                break;
180
            case 'array':
181
                $value = (array)$value;
182
                break;
183
        }
184
        $this->data[$name] = $value;
185
        return $this;
186
    }
187
188
    /**
189
     * Gets the event property, either from built-in event properties or the custom properties from event_properties.
190
     *
191
     * As with the set() method, for built-in event properties, can use camelcase OR underscore and either one will
192
     * work.  This is not the case for custom event properties however.
193
     *
194
     * For example, any of these calls will get the value of device_brand:
195
     *
196
     * <code>
197
     * $event->get('device_brand');
198
     * $event->get('deviceBrand');
199
     * // Magic methods work too:
200
     * $event->device_brand;
201
     * $event->deviceBrand;
202
     * </code>
203
     *
204
     * If no value found, returns null.
205
     *
206
     * @param string $name
207
     * @return mixed
208
     */
209
    public function get($name)
210
    {
211
        $name = $this->normalize($name);
212
        if (isset($this->data[$name])) {
213
            return $this->data[$name];
214
        } elseif (isset($this->data['event_properties'][$name])) {
215
            return $this->data['event_properties'][$name];
216
        }
217
        return null;
218
    }
219
220
    /**
221
     * Unset event property, either from built-in event properties or the custom properties from event_properties.
222
     *
223
     * As with the set() method, for built-in event properties, can use camelcase OR underscore and either one will
224
     * work.  This is not the case for custom event properties however.
225
     *
226
     * For example, any of these calls will unset the main built-in property device_brand:
227
     *
228
     * <code>
229
     * $event->unsetProperty('device_brand');
230
     * $event->unsetProperty('deviceBrand');
231
     * // Magic methods work too:
232
     * unset($event->device_brand);
233
     * unset($event->deviceBrand);
234
     * </code>
235
     *
236
     * @param string $name
237
     * @return \Zumba\Amplitude\Event
238
     */
239
    public function unsetProperty($name)
240
    {
241
        $name = $this->normalize($name);
242
        if (isset($this->availableVars[$name])) {
243
            unset($this->data[$name]);
244
        } elseif (isset($this->data['event_properties'])) {
245
            unset($this->data['event_properties'][$name]);
246
        }
247
        return $this;
248
    }
249
250
    /**
251
     * Check if event property is set, either from built-in event properties or custom properties from event_properties
252
     *
253
     * As with the set() method, for built-in event properties, can use camelcase OR underscore and either one will
254
     * work.  This is not the case for custom event properties however.
255
     *
256
     * For example, any of these calls will check if the built in property for device_brand is set:
257
     *
258
     * <code>
259
     * $event->isPropertySet('device_brand');
260
     * $event->isPropertySet('deviceBrand');
261
     * // Magic methods work too:
262
     * isset($event->device_brand);
263
     * isset($event->deviceBrand);
264
     * </code>
265
     *
266
     * @param string $name
267
     * @return \Zumba\Amplitude\Event
268
     */
269
    public function isPropertySet($name)
270
    {
271
        $name = $this->normalize($name);
272
        return isset($this->data[$name]) || isset($this->data['event_properties'][$name]);
273
    }
274
275
    /**
276
     * Magic method to set the value.
277
     *
278
     * See the set() method.
279
     *
280
     * @param string $name
281
     * @param string $value
282
     * @return void
283
     */
284
    public function __set($name, $value)
285
    {
286
        $this->set($name, $value);
287
    }
288
289
    /**
290
     * Magic method to get the value
291
     *
292
     * See the get() method
293
     *
294
     * @param string $name
295
     * @return mixed
296
     */
297
    public function __get($name)
298
    {
299
        return $this->get($name);
300
    }
301
302
    /**
303
     * Unset event property
304
     *
305
     * See the unsetProperty() method
306
     *
307
     * @param string $name
308
     */
309
    public function __unset($name)
310
    {
311
        $this->unsetProperty($name);
312
    }
313
314
    /**
315
     * Magic method to see if name is set
316
     *
317
     * Uses same normalization on the name as the set method, where it will match built-in properties for either
318
     * camelcased or underscore version of property
319
     *
320
     * @param string $name
321
     * @return boolean
322
     */
323
    public function __isset($name)
324
    {
325
        return $this->isPropertySet($name);
326
    }
327
328
    /**
329
     * Normalized the name, by attempting to camelcase / underscore it to see if it matches any built-in property names.
330
     *
331
     * If it matches a built-in property name, will return the normalized property name.  Otherwise returns the name
332
     * un-modified.
333
     *
334
     * @param string $name
335
     * @return string
336
     */
337
    protected function normalize($name)
338
    {
339
        if (isset($this->availableVars[$name])) {
340
            return $name;
341
        }
342
        if (preg_match('/^[a-zA-Z_]+$/', $name)) {
343
            // No spaces or unexpected vars, this could be camelcased version or underscore version of a built-in
344
            // var name, check to see if it matches
345
            $underscore = Inflector::underscore($name);
346
            if (isset($this->availableVars[$underscore])) {
347
                return $underscore;
348
            }
349
            // In case it is one of the camel-cased versions
350
            $camel = Inflector::camelCase($name);
351
            if (isset($this->availableVars[$camel])) {
352
                return $camel;
353
            }
354
        }
355
        // Could not find name, just use original un-altered, probably used in event_properties
356
        return $name;
357
    }
358
359
    /**
360
     * Convert the event to array format
361
     *
362
     * @return array
363
     */
364
    public function toArray()
365
    {
366
        return $this->data;
367
    }
368
369
    /**
370
     * JSON serialize
371
     *
372
     * @return array
373
     */
374
    public function jsonSerialize()
375
    {
376
        return $this->data;
377
    }
378
}
379