FakeHeaders   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
c 2
b 1
f 0
lcom 1
cbo 0
dl 0
loc 56
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parseIniFiles() 0 6 1
A setBrowserFile() 0 4 1
A setSystemFile() 0 4 1
A setLocaleFile() 0 4 1
A getUserAgent() 0 23 1
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