Passed
Pull Request — master (#59)
by
unknown
07:29
created

LaravelMixpanel::getData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 19
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 26
rs 9.6333
1
<?php namespace GeneaLabs\LaravelMixpanel;
2
3
use Illuminate\Http\Request;
4
use Illuminate\Support\Facades\App;
5
use Mixpanel;
6
use Sinergi\BrowserDetector\Browser;
7
use Sinergi\BrowserDetector\Device;
8
use Sinergi\BrowserDetector\Os;
9
10
class LaravelMixpanel extends Mixpanel
11
{
12
    private $defaults;
13
    private $request;
14
15
    public function __construct(Request $request, array $options = [])
16
    {
17
        $this->defaults = [
18
            'consumer' => config('services.mixpanel.consumer', 'socket'),
19
            'connect_timeout' => config('services.mixpanel.connect-timeout', 2),
20
            'timeout' => config('services.mixpanel.timeout', 2),
21
        ];
22
        $this->request = $request;
23
24
        parent::__construct(
25
            config('services.mixpanel.token'),
26
            array_merge($this->defaults, $options)
27
        );
28
    }
29
30
    public function getData()
31
    {
32
        $browserInfo = new Browser();
33
        $osInfo = new Os();
34
        $deviceInfo = new Device();
35
        $browserVersion = trim(str_replace('unknown', '', $browserInfo->getName() . ' ' . $browserInfo->getVersion()));
36
        $osVersion = trim(str_replace('unknown', '', $osInfo->getName() . ' ' . $osInfo->getVersion()));
37
        $hardwareVersion = trim(str_replace('unknown', '', $deviceInfo->getName()));
38
39
        $data = [
40
            'Url' => $this->request->getUri(),
41
            'Operating System' => $osVersion,
42
            'Hardware' => $hardwareVersion,
43
            '$browser' => $browserVersion,
44
            'Referrer' => $this->request->header('referer'),
45
            '$referring_domain' => ($this->request->header('referer')
46
                ? 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

46
                ? parse_url(/** @scrutinizer ignore-type */ $this->request->header('referer'))['host']
Loading history...
47
                : null),
48
            'ip' => $this->request->ip(),
49
        ];
50
51
        if ((! array_key_exists('$browser', $data)) && $browserInfo->isRobot()) {
52
            $data['$browser'] = 'Robot';
53
        }
54
55
        return $data;
56
    }
57
58
    public function track($event, $properties = [])
59
    {
60
        $data = array_filter($this->getData());
61
        $properties = array_filter($properties);
62
        
63
        parent::track($event, $properties + $data);
64
    }
65
}
66