Detector::getUserAgent()   A
last analyzed

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 endorphin-studio/browser-detector
8
 */
9
10
namespace EndorphinStudio\Detector;
11
12
use EndorphinStudio\Detector\Data\AbstractData;
13
use EndorphinStudio\Detector\Data\Result;
14
use EndorphinStudio\Detector\Exception\StorageException;
15
use EndorphinStudio\Detector\Storage\AbstractStorage;
16
use EndorphinStudio\Detector\Storage\StorageInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * Class Detector
21
 * Detect OS, Device, Browser, Robot
22
 * @package EndorphinStudio\Detector
23
 */
24
class Detector
25
{
26
    /**
27
     * @var array Array of options
28
     */
29
    protected $options = [
30
        'dataProvider' => '\\EndorphinStudio\\Detector\\Storage\\YamlStorage',
31
        'dataDirectory' => 'auto',
32
        'cacheDirectory' => 'auto',
33
        'format' => 'yaml'
34
    ];
35
36
    /**
37
     * @var StorageInterface
38
     */
39
    private $dataProvider;
40
41
    /**
42
     * Get storage provider
43
     * @return StorageInterface
44
     */
45
    public function getDataProvider(): StorageInterface
46
    {
47
        return $this->dataProvider;
48
    }
49
50
    /**
51
     * Get result object
52
     * @return Result Result object
53
     */
54
    public function getResultObject(): Result
55
    {
56
        return $this->resultObject;
57
    }
58
59
    /**
60
     * @var Result Result object
61
     */
62
    private $resultObject;
63
64
    /**
65
     * Set data provider
66
     * @param StorageInterface $dataProvider
67
     */
68
    public function setDataProvider(StorageInterface $dataProvider)
69
    {
70
        $this->dataProvider = $dataProvider;
71
    }
72
73
    /**
74
     * @return mixed
75
     */
76
    public function getUserAgent()
77
    {
78
        return $this->ua;
79
    }
80
81
    private $ua;
82
83
    /**
84
     * @var array
85
     */
86
    private $detectors;
87
88
    /**
89
     * Detector constructor.
90
     * Options:
91
     * 'dataProvider' => '\\EndorphinStudio\\Detector\\Storage\\YamlStorage',
92
     * 'dataDirectory' => 'auto',
93
     * 'cacheDirectory' => 'auto',
94
     * 'format' => 'yaml'
95
     * @param array $options Array of options
96
     * @throws \ReflectionException
97
     * @throws StorageException
98
     */
99
    public function __construct(array $options = [])
100
    {
101
        $this->options = array_merge_recursive($options, $this->options);
102
103
        $this->init();
104
        $this->detectors = [];
105
        $check = ['os','device', 'browser', 'robot'];
106
        Tools::setWindowsConfig($this->dataProvider->getConfig()['windows']);
107
        foreach ($check as $detectionType) {
108
            $className = sprintf('\\EndorphinStudio\\Detector\\Detection\\%s', ucfirst(sprintf('%sDetector', $detectionType)));
109
            if(class_exists($className)) {
110
                $this->detectors[$detectionType] = new $className();
111
                $this->detectors[$detectionType]->init($this);
112
            }
113
        }
114
    }
115
116
    /**
117
     * Analyse User Agent String
118
     * @param string $ua
119
     * @return Result
120
     */
121
    public function analyse(string $ua = 'ua'): Result
122
    {
123
        $request = Request::createFromGlobals();
124
        $this->ua = $ua === 'ua' ? $request->server->get('HTTP_USER_AGENT') : $ua;
125
        $this->resultObject = new Result($this->ua);
126
        foreach ($this->detectors as $detectionType => $detector) {
127
            $detector->detect();
128
        }
129
        return $this->resultObject;
130
    }
131
132
    /**
133
     * Get list of patterns from config
134
     * @param $list
135
     * @param $type
136
     * @return array
137
     */
138
    public function getPatternList($list, $type)
139
    {
140
        return array_key_exists($type, $list) ? $list[$type] : [];
141
    }
142
143
    /**
144
     * Initialisation method
145
     * @throws \ReflectionException
146
     * @throws StorageException
147
     */
148
    protected function init()
149
    {
150
        $dataProvider = new $this->options['dataProvider']();
151
152
        /** @var StorageInterface $dataProvider */
153
        $this->setDataProvider($dataProvider);
154
        $this->dataProvider->setDataDirectory($this->findDataDirectory());
155
        if(method_exists($this->dataProvider,'setCacheDirectory')) {
156
            $this->dataProvider->setCacheDirectory($this->findCacheDirectory());
0 ignored issues
show
Bug introduced by
The method setCacheDirectory() does not seem to exist on object<EndorphinStudio\D...orage\StorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
        }
158
        if(method_exists($this->dataProvider,'setCacheEnabled')) {
159
            $this->dataProvider->setCacheEnabled(true);
0 ignored issues
show
Bug introduced by
The method setCacheEnabled() does not seem to exist on object<EndorphinStudio\D...orage\StorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
160
        }
161
    }
162
163
    /**
164
     * @return string
165
     * @throws StorageException
166
     * @throws \ReflectionException
167
     */
168 View Code Duplication
    private function findDataDirectory(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170
        $dataDirectory = $this->options['dataDirectory'];
171
        if($this->options['dataDirectory'] === 'auto') {
172
            $reflection = new \ReflectionClass(AbstractData::class);
173
            $dataDirectory = sprintf('%s/var/%s', dirname($reflection->getFileName(),3), $this->options['format']);
174
        }
175
        if(is_dir($dataDirectory)){
176
            return $dataDirectory;
177
        }
178
        throw new StorageException(sprintf(StorageException::DIRECTORY_NOT_FOUND, $dataDirectory));
179
    }
180
181
    /**
182
     * @return string
183
     * @throws StorageException
184
     * @throws \ReflectionException
185
     */
186 View Code Duplication
    private function findCacheDirectory(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188
        $cacheDirectory = $this->options['cacheDirectory'];
189
        if($this->options['cacheDirectory'] === 'auto') {
190
            $reflection = new \ReflectionClass(AbstractData::class);
191
            $cacheDirectory = sprintf('%s/var/cache', dirname($reflection->getFileName(),3));
192
        }
193
        if(is_dir($cacheDirectory)){
194
            return $cacheDirectory;
195
        }
196
        throw new StorageException(sprintf(StorageException::DIRECTORY_NOT_FOUND, $cacheDirectory));
197
    }
198
}