FakeHeaders::getUserAgent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace Aszone\FakeHeaders;
4
5
class FakeHeaders
6
{
7
    private $browserFile = __DIR__.'/../resources/UserAgent/Browser.ini';
8
9
    private $systemFile = __DIR__.'/../resources/UserAgent/System.ini';
10
11
    private $localeFile = __DIR__.'/../resources/UserAgent/Locale.ini';
12
13
    private $browser, $system, $locale;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
14
15
    private function parseIniFiles()
16
    {
17
        $this->browser = parse_ini_file($this->browserFile);
18
        $this->system = parse_ini_file($this->systemFile);
19
        $this->locale = parse_ini_file($this->localeFile);
20
    }
21
22
    public function setBrowserFile($file)
23
    {
24
        $this->browserFile = $file;
25
    }
26
27
    public function setSystemFile($file)
28
    {
29
        $this->systemFile = $file;
30
    }
31
32
    public function setLocaleFile($file)
33
    {
34
        $this->localeFile = $file;
35
    }
36
37
    public function getUserAgent()
38
    {
39
        $this->parseIniFiles();
40
41
        $randBrowser = $this->browser[rand(0, count($this->browser) - 1)];
42
        $randSystem = $this->system[rand(0, count($this->system) - 1)];
43
        $randLocale = $this->locale[rand(0, count($this->locale) - 1)];
44
45
        $format = '%s/%d.%d (%s %d.%d; %s;)';
46
47
        $userAgent = sprintf(
48
            $format,
49
            $randBrowser,
50
            rand(1, 20),
51
            rand(0, 20),
52
            $randSystem,
53
            rand(1, 7),
54
            rand(0, 9),
55
            $randLocale
56
        );
57
58
        return ['User-Agent' => $userAgent];
59
    }
60
}
61