Completed
Push — 4.0 ( 5b028b...8f794f )
by Serhii
12s
created

Detector::setDataProvider()   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 1
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 endorphin-studio/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\AbstractStorage;
15
use EndorphinStudio\Detector\Storage\StorageInterface;
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * Class Detector
20
 * Detect OS, Device, Browser, Robot
21
 * @package EndorphinStudio\Detector
22
 */
23
class Detector
24
{
25
    /**
26
     * @var StorageInterface
27
     */
28
    private $dataProvider;
29
30
    /**
31
     * Get storage provider
32
     * @return StorageInterface
33
     */
34
    public function getDataProvider(): StorageInterface
35
    {
36
        return $this->dataProvider;
37
    }
38
39
    /**
40
     * Get result object
41
     * @return Result Result object
42
     */
43
    public function getResultObject(): Result
44
    {
45
        return $this->resultObject;
46
    }
47
48
    /**
49
     * @var Result Result object
50
     */
51
    private $resultObject;
52
53
    /**
54
     * Set data provider
55
     * @param StorageInterface $dataProvider
56
     */
57
    public function setDataProvider(StorageInterface $dataProvider)
58
    {
59
        $this->dataProvider = $dataProvider;
60
    }
61
62
    /**
63
     * @return mixed
64
     */
65
    public function getUserAgent()
66
    {
67
        return $this->ua;
68
    }
69
70
    private $ua;
71
72
    /**
73
     * @var array
74
     */
75
    private $detectors;
76
77
    /**
78
     * Detector constructor.
79
     * @param string $dataProvider
80
     * @param string $format
81
     */
82
    public function __construct(string $dataProvider = '\\EndorphinStudio\\Detector\\Storage\\YamlStorage', string $format = 'yaml')
83
    {
84
        $dataDirectory = sprintf('%s/var/%s', dirname(__DIR__), $format);
85
        /** @var StorageInterface $dataProvider */
86
        $dataProvider = new $dataProvider();
87
        $dataProvider->setDataDirectory($dataDirectory);
88
        $this->setDataProvider($dataProvider);
89
        $this->detectors = [];
90
        $check = ['os','device', 'browser', 'robot'];
91
        Tools::setWindowsConfig($dataProvider->getConfig()['windows']);
92
        foreach ($check as $detectionType) {
93
            $className = sprintf('\\EndorphinStudio\\Detector\\Detection\\%s', ucfirst(sprintf('%sDetector', $detectionType)));
94
            if(class_exists($className)) {
95
                $this->detectors[$detectionType] = new $className();
96
                $this->detectors[$detectionType]->init($this);
97
            }
98
        }
99
    }
100
101
    /**
102
     * Analyse User Agent String
103
     * @param string $ua
104
     * @return Result
105
     */
106
    public function analyse(string $ua = 'ua'): Result
107
    {
108
        $request = Request::createFromGlobals();
109
        $this->ua = $ua === 'ua' ? $request->server->get('HTTP_USER_AGENT') : $ua;
110
        $this->resultObject = new Result($this->ua);
111
        foreach ($this->detectors as $detectionType => $detector) {
112
            $detector->detect($ua);
113
        }
114
        return $this->resultObject;
115
    }
116
117
    /**
118
     * Get list of patterns from config
119
     * @param $list
120
     * @param $type
121
     * @return array
122
     */
123
    public function getPatternList($list, $type)
124
    {
125
        return array_key_exists($type, $list) ? $list[$type] : [];
126
    }
127
}