LaravelMixpanel   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 1
Metric Value
eloc 38
c 9
b 0
f 1
dl 0
loc 62
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A getData() 0 26 4
A track() 0 11 2
1
<?php
2
3
namespace GeneaLabs\LaravelMixpanel;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\App;
7
use Mixpanel;
8
use Sinergi\BrowserDetector\Browser;
9
use Sinergi\BrowserDetector\Device;
10
use Sinergi\BrowserDetector\Os;
11
12
class LaravelMixpanel extends Mixpanel
13
{
14
    private $callbackResults;
15
    private $defaults;
16
    private $request;
17
18
    public function __construct(Request $request, array $options = [])
19
    {
20
        $this->callbackResults = [];
21
        $this->defaults = [
22
            'consumer' => config('services.mixpanel.consumer', 'socket'),
23
            'connect_timeout' => config('services.mixpanel.connect-timeout', 2),
24
            'timeout' => config('services.mixpanel.timeout', 2),
25
        ];
26
        $this->request = $request;
27
28
29
        parent::__construct(
30
            config('services.mixpanel.token'),
31
            array_merge($this->defaults, $options)
32
        );
33
    }
34
35
    protected function getData() : array
36
    {
37
        $browserInfo = new Browser();
38
        $osInfo = new Os();
39
        $deviceInfo = new Device();
40
        $browserVersion = trim(str_replace('unknown', '', $browserInfo->getName() . ' ' . $browserInfo->getVersion()));
41
        $osVersion = trim(str_replace('unknown', '', $osInfo->getName() . ' ' . $osInfo->getVersion()));
42
        $hardwareVersion = trim(str_replace('unknown', '', $deviceInfo->getName()));
43
44
        $data = [
45
            'Url' => $this->request->getUri(),
46
            'Operating System' => $osVersion,
47
            'Hardware' => $hardwareVersion,
48
            '$browser' => $browserVersion,
49
            'Referrer' => $this->request->header('referer'),
50
            '$referring_domain' => ($this->request->header('referer')
51
                ? parse_url($this->request->header('referer'))['host']
0 ignored issues
show
Bug introduced by
It seems like $this->request->header('referer') can also be of type array; however, parameter $url of parse_url() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
                ? parse_url(/** @scrutinizer ignore-type */ $this->request->header('referer'))['host']
Loading history...
52
                : null),
53
            'ip' => $this->request->ip(),
54
        ];
55
56
        if ((! array_key_exists('$browser', $data)) && $browserInfo->isRobot()) {
57
            $data['$browser'] = 'Robot';
58
        }
59
60
        return array_filter($data);
61
    }
62
63
    public function track($event, $properties = [])
64
    {
65
        $properties = array_filter($properties);
66
        $data = $properties + $this->getData();
67
68
        if ($callbackClass = config("services.mixpanel.data_callback_class")) {
69
            $data = (new $callbackClass)->process($data);
70
            $data = array_filter($data);
71
        }
72
        
73
        parent::track($event, $data);
74
    }
75
}
76