Completed
Pull Request — master (#25)
by
unknown
04:51
created

Record::getVisitorOperatingSystem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 0
dl 0
loc 21
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace awssat\Visits\Traits;
4
5
use Spatie\Referer\Referer;
6
7
trait Record
8
{
9
    /**
10
     * @param $inc
11
     */
12
    protected function recordCountry($inc)
13
    {
14
        $this->redis->zincrby($this->keys->visits."_countries:{$this->keys->id}", $inc, $this->getVisitorCountry());
15
    }
16
17
    /**
18
     * @param $inc
19
     */
20
    protected function recordRefer($inc)
21
    {
22
        $referer = app(Referer::class)->get();
23
        $this->redis->zincrby($this->keys->visits."_referers:{$this->keys->id}", $inc, $referer);
24
    }
25
26
    /**
27
     * @param $inc
28
     */
29
    protected function recordOperatingSystem($inc)
30
    {
31
        $this->redis->zincrby($this->keys->visits."_OSes:{$this->keys->id}", $inc, $this->getVisitorOperatingSystem());
32
    }
33
34
    /**
35
     * @param $inc
36
     */
37
    protected function recordLanguage($inc)
38
    {
39
        $this->redis->zincrby($this->keys->visits."_languages:{$this->keys->id}", $inc, $this->getVisitorLanguage());
40
    }
41
42
    /**
43
     * @param $inc
44
     */
45
    protected function recordPeriods($inc)
46
    {
47
        foreach ($this->periods as $period) {
48
            $periodKey = $this->keys->period($period);
49
50
            $this->redis->zincrby($periodKey, $inc, $this->keys->id);
51
            $this->redis->incrby($periodKey.'_total', $inc);
52
        }
53
    }
54
55
    /**
56
     *  Gets visitor country code
57
     * @return mixed|string
58
     */
59
    protected function getVisitorCountry()
60
    {
61
        return strtolower(geoip()->getLocation()->iso_code);
0 ignored issues
show
Bug introduced by
The method getLocation() does not exist on Torann\GeoIP\Location. ( Ignorable by Annotation )

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

61
        return strtolower(geoip()->/** @scrutinizer ignore-call */ getLocation()->iso_code);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
    }
63
64
    /**
65
     *  Gets visitor operating system
66
     * @return mixed|string
67
     */
68
    public function getVisitorOperatingSystem()
69
    {
70
        $osArray = [
71
        '/windows|win32|win16|win95/i' => 'Windows',
72
        '/iphone/i' => 'iPhone',
73
        '/ipad/i' => 'iPad',
74
        '/macintosh|mac os x|mac_powerpc/i' => 'MacOS',
75
        '/(?=.*mobile)android/i' => 'AndroidMobile',
76
        '/(?!.*mobile)android/i' => 'AndroidTablet',
77
        '/android/i' => 'Android',
78
        '/blackberry/i' => 'BlackBerry',
79
        '/linux/i' => 'Linux',
80
        ];
81
82
        foreach ($osArray as $regex => $value) {
83
            if (preg_match($regex, request()->server('HTTP_USER_AGENT') ?? '')) {
84
                return $value;
85
            }
86
        }
87
88
        return 'unknown';
89
    }
90
91
    /**
92
     *  Gets visitor language
93
     * @return mixed|string
94
     */
95
    public function getVisitorLanguage()
96
    {
97
98
        return request()->getPreferredLanguage();
99
    }
100
}
101