Record::getVisitorReferer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 1
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->connection->increment($this->keys->visits."_countries:{$this->keys->id}", $inc, $this->getVisitorCountry());
15
    }
16
17
    /**
18
     * @param $inc
19
     */
20
    protected function recordRefer($inc)
21
    {
22
        $this->connection->increment($this->keys->visits."_referers:{$this->keys->id}", $inc, $this->getVisitorReferer());
23
    }
24
25
    /**
26
     * @param $inc
27
     */
28
    protected function recordOperatingSystem($inc)
29
    {
30
        $this->connection->increment($this->keys->visits."_OSes:{$this->keys->id}", $inc, $this->getVisitorOperatingSystem());
31
    }
32
33
    /**
34
     * @param $inc
35
     */
36
    protected function recordLanguage($inc)
37
    {
38
        $this->connection->increment($this->keys->visits."_languages:{$this->keys->id}", $inc, $this->getVisitorLanguage());
39
    }
40
41
    /**
42
     * @param $inc
43
     */
44
    protected function recordPeriods($inc)
45
    {
46
        foreach ($this->periods as $period) {
47
            $periodKey = $this->keys->period($period);
48
49
            $this->connection->increment($periodKey, $inc, $this->keys->id);
50
            $this->connection->increment($periodKey.'_total', $inc);
51
        }
52
    }
53
54
    /**
55
     *  Gets visitor country code
56
     * @return mixed|string
57
     */
58
    public function getVisitorCountry()
59
    {
60
        //In case of using unsupported cache driver. Although 'country' is globally  
61
        //ignored already, we can not rely on user awareness of geoIP package restriction.
62
        if (
63
            in_array(config('cache.default'),  ['file', 'dynamodb', 'database']) &&
64
            is_array($geoipTags = config('geoip.cache_tags')) && count($geoipTags) > 0
65
        ) {
66
            return null;
67
        }
68
69
        return strtolower(geoip()->getLocation()->iso_code);
0 ignored issues
show
Bug introduced by
It seems like geoip()->getLocation()->iso_code can also be of type null; however, parameter $string of strtolower() 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

69
        return strtolower(/** @scrutinizer ignore-type */ geoip()->getLocation()->iso_code);
Loading history...
70
    }
71
72
    /**
73
     *  Gets visitor operating system
74
     * @return mixed|string
75
     */
76
    public function getVisitorOperatingSystem()
77
    {
78
        $osArray = [
79
        '/windows|win32|win16|win95/i' => 'Windows',
80
        '/iphone/i' => 'iPhone',
81
        '/ipad/i' => 'iPad',
82
        '/macintosh|mac os x|mac_powerpc/i' => 'MacOS',
83
        '/(?=.*mobile)android/i' => 'AndroidMobile',
84
        '/(?!.*mobile)android/i' => 'AndroidTablet',
85
        '/android/i' => 'Android',
86
        '/blackberry/i' => 'BlackBerry',
87
        '/linux/i' => 'Linux',
88
        ];
89
90
        foreach ($osArray as $regex => $value) {
91
            if (preg_match($regex, request()->server('HTTP_USER_AGENT') ?? '')) {
0 ignored issues
show
Bug introduced by
It seems like request()->server('HTTP_USER_AGENT') ?? '' can also be of type array; however, parameter $subject of preg_match() 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

91
            if (preg_match($regex, /** @scrutinizer ignore-type */ request()->server('HTTP_USER_AGENT') ?? '')) {
Loading history...
92
                return $value;
93
            }
94
        }
95
96
        return 'unknown';
97
    }
98
99
    /**
100
     *  Gets visitor language
101
     * @return mixed|string
102
     */
103
    public function getVisitorLanguage()
104
    {
105
        $language = request()->getPreferredLanguage();
106
        if (false !== $position = strpos($language, '_')) {
0 ignored issues
show
Bug introduced by
It seems like $language can also be of type null; however, parameter $haystack of strpos() 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

106
        if (false !== $position = strpos(/** @scrutinizer ignore-type */ $language, '_')) {
Loading history...
107
            $language = substr($language, 0, $position);
0 ignored issues
show
Bug introduced by
It seems like $language can also be of type null; however, parameter $string of substr() 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

107
            $language = substr(/** @scrutinizer ignore-type */ $language, 0, $position);
Loading history...
108
        }
109
        return $language;
110
    }
111
112
    /**
113
     *  Gets visitor referer
114
     * @return mixed|string
115
     */
116
    public function getVisitorReferer()
117
    {
118
        return app(Referer::class)->get();
119
    }
120
}
121