Completed
Push — 4.0 ( e9991b...604d5a )
by Serhii
02:10
created

Detector::detect()   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 2
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
16
class Detector
17
{
18
    /**
19
     * @var StorageInterface
20
     */
21
    private $dataProvider;
22
23
    /**
24
     * @return StorageInterface
25
     */
26
    public function getDataProvider(): StorageInterface
27
    {
28
        return $this->dataProvider;
29
    }
30
31
    /**
32
     * @return Result
33
     */
34
    public function getResultObject(): Result
35
    {
36
        return $this->resultObject;
37
    }
38
39
    /**
40
     * @var Result
41
     */
42
    private $resultObject;
43
44
    /**
45
     * @param StorageInterface $dataProvider
46
     */
47
    public function setDataProvider(StorageInterface $dataProvider)
48
    {
49
        $this->dataProvider = $dataProvider;
50
    }
51
52
    /**
53
     * @return mixed
54
     */
55
    public function getUserAgent()
56
    {
57
        return $this->ua;
58
    }
59
60
    private $ua;
61
62
    /**
63
     * @var array
64
     */
65
    private $detectors;
66
67
    /**
68
     * Detector constructor.
69
     * @param string $dataProvider
70
     * @param string $format
71
     * @throws StorageException
72
     */
73
    public function __construct(string $dataProvider = '\\EndorphinStudio\\Detector\\Storage\\YamlStorage', string $format = 'yaml')
74
    {
75
        $dataDirectory = sprintf('%s/var/%s', dirname(__DIR__), $format);
76
        $dataProvider = new $dataProvider();
77
        $dataProvider->setDataDirectory($dataDirectory);
78
        $this->setDataProvider($dataProvider);
79
        $this->detectors = [];
80
        $this->resultObject = new Result();
81
        $check = ['os','device', 'browser', 'robots'];
82
        foreach ($check as $detectionType) {
83
            $className = sprintf('\\EndorphinStudio\\Detector\\Detection\\%s', ucfirst(sprintf('%sDetector', $detectionType)));
84
            if(class_exists($className)) {
85
                $this->detectors[$detectionType] = new $className();
86
                $this->detectors[$detectionType]->init($this);
87
            }
88
        }
89
    }
90
91
    public function analyze(string $ua = 'ua')
92
    {
93
        $this->ua = $ua === 'ua' ? $_SERVER['HTTP_USER_AGENT'] : $ua;
94
        foreach ($this->detectors as $detectionType => $detector) {
95
            $detector->detect($ua);
96
        }
97
        die();
98
    }
99
100
    public function getPatternList($list, $type)
101
    {
102
        return array_key_exists($type, $list) ? $list[$type] : [];
103
    }
104
}