UserAgent   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Test Coverage

Coverage 98.81%

Importance

Changes 0
Metric Value
wmc 13
eloc 75
dl 0
loc 166
ccs 83
cts 84
cp 0.9881
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A androidMobileToken() 0 3 1
A chrome() 0 14 1
A linuxPlatformToken() 0 3 1
A firefox() 0 13 1
A safari() 0 25 2
A userAgent() 0 5 1
A internetExplorer() 0 4 1
A opera() 0 10 1
A windowsPlatformToken() 0 3 1
A macPlatformToken() 0 4 1
A edge() 0 20 1
A iosMobileToken() 0 5 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace DummyGenerator\Core;
6
7
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionInterface;
8
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionTrait;
9
use DummyGenerator\Definitions\Extension\UserAgentExtensionInterface;
10
11
class UserAgent implements UserAgentExtensionInterface, RandomizerAwareExtensionInterface
12
{
13
    use RandomizerAwareExtensionTrait;
14
15
    /** @var string[] */
16
    protected array $userAgents = ['firefox', 'chrome', 'internetExplorer', 'opera', 'safari', 'edge'];
17
18
    /** @var string[] */
19
    protected array $windowsPlatformTokens = [
20
        'Windows NT 6.2', 'Windows NT 6.1', 'Windows NT 6.0', 'Windows NT 5.2', 'Windows NT 5.1',
21
        'Windows NT 5.01', 'Windows NT 5.0', 'Windows NT 4.0', 'Windows 98; Win 9x 4.90', 'Windows 98',
22
        'Windows 95', 'Windows CE',
23
    ];
24
25
    /**
26
     * @var string[]
27
     *
28
     * Possible processors on Linux
29
     */
30
    protected array $linuxProcessor = ['i686', 'x86_64'];
31
32
    /**
33
     * @var string[]
34
     *
35
     * Mac processors (it also added U;)
36
     */
37
    protected array $macProcessor = ['Intel', 'PPC', 'U; Intel', 'U; PPC'];
38
39
    /**
40
     * @var string[]
41
     *
42
     * Add as many languages as you like.
43
     */
44
    protected array $lang = ['en-US', 'sl-SI', 'nl-NL'];
45
46 1
    public function userAgent(): string
47
    {
48 1
        $userAgentName = $this->randomizer->randomElement($this->userAgents);
49
50 1
        return $this->$userAgentName();
51
    }
52
53 1
    public function chrome(): string
54
    {
55 1
        $saf = $this->randomizer->getInt(531, 536) . $this->randomizer->getInt(0, 2);
56
57 1
        $platforms = [
58 1
            '(' . $this->linuxPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/" . $this->randomizer->getInt(36, 40) . '.0.'
59 1
                . $this->randomizer->getInt(800, 899) . ".0 Mobile Safari/$saf",
60 1
            '(' . $this->windowsPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/" . $this->randomizer->getInt(36, 40) . '.0.'
61 1
                . $this->randomizer->getInt(800, 899) . ".0 Mobile Safari/$saf",
62 1
            '(' . $this->macPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/" . $this->randomizer->getInt(36, 40) . '.0.'
63 1
                . $this->randomizer->getInt(800, 899) . ".0 Mobile Safari/$saf",
64 1
        ];
65
66 1
        return 'Mozilla/5.0 ' . $this->randomizer->randomElement($platforms);
67
    }
68
69 1
    public function edge(): string
70
    {
71 1
        $saf = $this->randomizer->getInt(531, 537) . '.' . $this->randomizer->getInt(0, 2);
72 1
        $chrv = $this->randomizer->getInt(79, 99) . '.0';
73
74 1
        $platforms = [
75 1
            '(' . $this->windowsPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/$chrv" . '.' . $this->randomizer->getInt(4000, 4844)
76 1
                . '.' . $this->randomizer->getInt(10, 99) . " Safari/$saf Edg/$chrv" . $this->randomizer->getInt(1000, 1146) . '.'
77 1
                . $this->randomizer->getInt(0, 99),
78 1
            '(' . $this->macPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/$chrv" . '.' . $this->randomizer->getInt(4000, 4844)
79 1
                . '.' . $this->randomizer->getInt(10, 99) . " Safari/$saf Edg/$chrv" . $this->randomizer->getInt(1000, 1146)
80 1
                . '.' . $this->randomizer->getInt(0, 99),
81 1
            '(' . $this->linuxPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/$chrv" . '.' . $this->randomizer->getInt(4000, 4844)
82 1
                . '.' . $this->randomizer->getInt(10, 99) . " Safari/$saf EdgA/$chrv" . $this->randomizer->getInt(1000, 1146)
83 1
                . '.' . $this->randomizer->getInt(0, 99),
84 1
            '(' . $this->iosMobileToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Version/15.0 EdgiOS/$chrv" . $this->randomizer->getInt(1000, 1146)
85 1
                . '.' . $this->randomizer->getInt(0, 99) . " Mobile/15E148 Safari/$saf",
86 1
        ];
87
88 1
        return 'Mozilla/5.0 ' . $this->randomizer->randomElement($platforms);
89
    }
90
91 1
    public function firefox(): string
92
    {
93 1
        $ver = 'Gecko/' . date('Ymd', $this->randomizer->getInt(strtotime('2010-1-1'), time())) . ' Firefox/'
94 1
            . $this->randomizer->getInt(35, 37) . '.0';
95
96 1
        $platforms = [
97 1
            '(' . $this->windowsPlatformToken() . '; ' . $this->randomizer->randomElement($this->lang) . '; rv:1.9.' . $this->randomizer->getInt(0, 2)
98 1
                . '.20) ' . $ver,
99 1
            '(' . $this->linuxPlatformToken() . '; rv:' . $this->randomizer->getInt(5, 7) . '.0) ' . $ver,
100 1
            '(' . $this->macPlatformToken() . ' rv:' . $this->randomizer->getInt(2, 6) . '.0) ' . $ver,
101 1
        ];
102
103 1
        return 'Mozilla/5.0 ' . $this->randomizer->randomElement($platforms);
104
    }
105
106 2
    public function safari(): string
107
    {
108 2
        $saf = $this->randomizer->getInt(531, 535) . '.' . $this->randomizer->getInt(1, 50) . '.' . $this->randomizer->getInt(1, 7);
109
110 2
        $ver = $this->randomizer->getBool() ?
111 2
            $this->randomizer->getInt(4, 5) . '.' . $this->randomizer->getInt(0, 1)
112 2
            :
113
            $this->randomizer->getInt(4, 5) . '.0.' . $this->randomizer->getInt(1, 5);
114
115 2
        $mobileDevices = [
116 2
            'iPhone; CPU iPhone OS',
117 2
            'iPad; CPU OS',
118 2
        ];
119
120 2
        $platforms = [
121 2
            '(Windows; U; ' . $this->windowsPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Version/$ver Safari/$saf",
122 2
            '(' . $this->macPlatformToken() . ' rv:' . $this->randomizer->getInt(2, 6) . '.0; ' . $this->randomizer->randomElement($this->lang)
123 2
                . ") AppleWebKit/$saf (KHTML, like Gecko) Version/$ver Safari/$saf",
124 2
            '(' . $this->randomizer->randomElement($mobileDevices) . ' ' . $this->randomizer->getInt(7, 8) . '_' . $this->randomizer->getInt(0, 2)
125 2
                . '_' . $this->randomizer->getInt(1, 2) . ' like Mac OS X; ' . $this->randomizer->randomElement($this->lang)
126 2
                . ") AppleWebKit/$saf (KHTML, like Gecko) Version/" . $this->randomizer->getInt(3, 4) . '.0.5 Mobile/8B'
127 2
                . $this->randomizer->getInt(111, 119) . " Safari/6$saf",
128 2
        ];
129
130 2
        return 'Mozilla/5.0 ' . $this->randomizer->randomElement($platforms);
131
    }
132
133 1
    public function opera(): string
134
    {
135 1
        $platforms = [
136 1
            '(' . $this->linuxPlatformToken() . '; ' . $this->randomizer->randomElement($this->lang) . ') Presto/2.' . $this->randomizer->getInt(8, 12)
137 1
                . '.' . $this->randomizer->getInt(160, 355) . ' Version/' . $this->randomizer->getInt(10, 12) . '.00',
138 1
            '(' . $this->windowsPlatformToken() . '; ' . $this->randomizer->randomElement($this->lang) . ') Presto/2.' . $this->randomizer->getInt(8, 12)
139 1
                . '.' . $this->randomizer->getInt(160, 355) . ' Version/' . $this->randomizer->getInt(10, 12) . '.00',
140 1
        ];
141
142 1
        return 'Opera/' . $this->randomizer->getInt(8, 9) . '.' . $this->randomizer->getInt(10, 99) . ' ' . $this->randomizer->randomElement($platforms);
143
    }
144
145 1
    public function internetExplorer(): string
146
    {
147 1
        return 'Mozilla/5.0 (compatible; MSIE ' . $this->randomizer->getInt(5, 11) . '.0; ' . $this->windowsPlatformToken() . '; Trident/'
148 1
                . $this->randomizer->getInt(3, 5) . '.' . $this->randomizer->getInt(0, 1) . ')';
149
    }
150
151 8
    public function windowsPlatformToken(): string
152
    {
153 8
        return $this->randomizer->randomElement($this->windowsPlatformTokens);
154
    }
155
156 6
    public function macPlatformToken(): string
157
    {
158 6
        return 'Macintosh; ' . $this->randomizer->randomElement($this->macProcessor) . ' Mac OS X 10_' . $this->randomizer->getInt(5, 8)
159 6
                . '_' . $this->randomizer->getInt(0, 9);
160
    }
161
162 2
    public function iosMobileToken(): string
163
    {
164 2
        $iosVer = $this->randomizer->getInt(13, 15) . '_' . $this->randomizer->getInt(0, 2);
165
166 2
        return 'iPhone; CPU iPhone OS ' . $iosVer . ' like Mac OS X';
167
    }
168
169 1
    public function androidMobileToken(): string
170
    {
171 1
        return 'Linux; Android ' . $this->randomizer->getInt(8, 15);
172
    }
173
174 5
    public function linuxPlatformToken(): string
175
    {
176 5
        return 'X11; Linux ' . $this->randomizer->randomElement($this->linuxProcessor);
177
    }
178
}
179