Completed
Push — 4.0 ( c710b5...5960ab )
by Serhii
02:22
created

Detector::getResultObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @author Serhii Nekhaienko <[email protected]>
4
 * @license GPL
5
 * @copyright Serhii Nekhaienko &copy 2018
6
 * @version 4.0.0
7
 * @project browser-detector
8
 */
9
10
namespace EndorphinStudio\Detector;
11
12
use EndorphinStudio\Detector\Data\Result;
13
use EndorphinStudio\Detector\Exception\StorageException;
14
use EndorphinStudio\Detector\Storage\StorageInterface;
15
use Twig\Parser;
16
17
class Detector
18
{
19
    /**
20
     * @var StorageInterface
21
     */
22
    private $dataProvider;
23
24
    /**
25
     * @return StorageInterface
26
     */
27
    public function getDataProvider(): StorageInterface
28
    {
29
        return $this->dataProvider;
30
    }
31
32
    /**
33
     * @return Result
34
     */
35
    public function getResultObject(): Result
36
    {
37
        return $this->resultObject;
38
    }
39
40
    /**
41
     * @var Result
42
     */
43
    private $resultObject;
44
45
    /**
46
     * @param StorageInterface $dataProvider
47
     */
48
    public function setDataProvider(StorageInterface $dataProvider)
49
    {
50
        $this->dataProvider = $dataProvider;
51
    }
52
53
    /**
54
     * @return mixed
55
     */
56
    public function getUserAgent()
57
    {
58
        return $this->ua;
59
    }
60
61
    private $ua;
62
63
    /**
64
     * @var array
65
     */
66
    private $detectors;
67
68
    /**
69
     * Detector constructor.
70
     * @param string $dataProvider
71
     * @param string $format
72
     * @throws StorageException
73
     */
74
    public function __construct(string $dataProvider = '\\EndorphinStudio\\Detector\\Storage\\YamlStorage', string $format = 'yaml')
75
    {
76
        $dataDirectory = sprintf('%s/var/%s', dirname(__DIR__), $format);
77
        $dataProvider = new $dataProvider();
78
        $dataProvider->setDataDirectory($dataDirectory);
79
        $this->setDataProvider($dataProvider);
80
        $this->detectors = [];
81
        $this->resultObject = new Result();
82
        $check = ['device', 'os', 'browser', 'robots'];
83
        foreach ($check as $detectionnType) {
84
            $className = sprintf('\\EndorphinStudio\\Detector\\Detection\\%s', ucfirst(sprintf('%sDetector', $detectionnType)));
85
            if(class_exists($className)) {
86
                $this->detectors[$detectionnType] = new $className();
87
                $this->detectors[$detectionnType]->init($this);
88
            }
89
        }
90
    }
91
92
    public function analyze(string $ua = 'ua')
93
    {
94
        $this->ua = $ua === 'ua' ? $_SERVER['HTTP_USER_AGENT'] : $ua;
95
        $config = $this->getDataProvider()->getConfig();
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
96
        foreach ($this->detectors as $detectionType => $detector) {
97
            $detector->detect($ua);
98
        }
99
        echo 'test';
100
//        $this->resultObject = new Result();
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
//        foreach ($check as $type) {
102
//            $this->detect($config, $type);
103
//        }
104
//        var_dump($this->resultObject);
105
    }
106
107
    private function detect($config, $type)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
110
    }
111
112
    public function getPatternList($list, $type)
113
    {
114
        return array_key_exists($type, $list) ? $list[$type] : [];
115
    }
116
}