Completed
Push — fix-dep ( 69d40a...aa2849 )
by Serhii
01:20
created

Detector::getVersion()   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 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
    private $version = '4.0.5';
27
28
    public function getVersion(): string
29
    {
30
        return $this->version;
31
    }
32
33
    /**
34
     * @var array Array of options
35
     */
36
    protected $options = [
37
        'dataProvider' => '\\EndorphinStudio\\Detector\\Storage\\YamlStorage',
38
        'dataDirectory' => 'auto',
39
        'cacheDirectory' => 'auto',
40
        'format' => 'yaml'
41
    ];
42
43
    /**
44
     * @var StorageInterface
45
     */
46
    private $dataProvider;
47
48
    /**
49
     * Get storage provider
50
     * @return StorageInterface
51
     */
52
    public function getDataProvider(): StorageInterface
53
    {
54
        return $this->dataProvider;
55
    }
56
57
    /**
58
     * Get result object
59
     * @return Result Result object
60
     */
61
    public function getResultObject(): Result
62
    {
63
        return $this->resultObject;
64
    }
65
66
    /**
67
     * @var Result Result object
68
     */
69
    private $resultObject;
70
71
    /**
72
     * Set data provider
73
     * @param StorageInterface $dataProvider
74
     */
75
    public function setDataProvider(StorageInterface $dataProvider)
76
    {
77
        $this->dataProvider = $dataProvider;
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83
    public function getUserAgent()
84
    {
85
        return $this->ua;
86
    }
87
88
    private $ua;
89
90
    /**
91
     * @var array
92
     */
93
    private $detectors;
94
95
    /**
96
     * Detector constructor.
97
     * Options:
98
     * 'dataProvider' => '\\EndorphinStudio\\Detector\\Storage\\YamlStorage',
99
     * 'dataDirectory' => 'auto',
100
     * 'cacheDirectory' => 'auto',
101
     * 'format' => 'yaml'
102
     * @param array $options Array of options
103
     * @throws \ReflectionException
104
     * @throws StorageException
105
     */
106
    public function __construct(array $options = [])
107
    {
108
        $this->options = array_merge_recursive($options, $this->options);
109
110
        $this->init();
111
        $this->detectors = [];
112
        $check = ['os','device', 'browser', 'robot'];
113
        Tools::setWindowsConfig($this->dataProvider->getConfig()['windows']);
114
        foreach ($check as $detectionType) {
115
            $className = sprintf('\\EndorphinStudio\\Detector\\Detection\\%s', ucfirst(sprintf('%sDetector', $detectionType)));
116
            if(class_exists($className)) {
117
                $this->detectors[$detectionType] = new $className();
118
                $this->detectors[$detectionType]->init($this);
119
            }
120
        }
121
    }
122
123
    /**
124
     * Analyse User Agent String
125
     * @param string $ua
126
     * @return Result
127
     */
128
    public function analyse(string $ua = 'ua'): Result
129
    {
130
        $request = Request::createFromGlobals();
131
        $this->ua = $ua === 'ua' ? $request->server->get('HTTP_USER_AGENT') : $ua;
132
        $this->resultObject = new Result($this->ua, $this);
133
        foreach ($this->detectors as $detectionType => $detector) {
134
            $detector->detect();
135
        }
136
        return $this->resultObject;
137
    }
138
139
    /**
140
     * Get list of patterns from config
141
     * @param $list
142
     * @param $type
143
     * @return array
144
     */
145
    public function getPatternList($list, $type)
146
    {
147
        return array_key_exists($type, $list) ? $list[$type] : [];
148
    }
149
150
    /**
151
     * Initialisation method
152
     * @throws \ReflectionException
153
     * @throws StorageException
154
     */
155
    protected function init()
156
    {
157
        $dataProvider = new $this->options['dataProvider']();
158
159
        /** @var StorageInterface $dataProvider */
160
        $this->setDataProvider($dataProvider);
161
        $this->dataProvider->setDataDirectory($this->findDataDirectory());
162
        if(method_exists($this->dataProvider,'setCacheDirectory')) {
163
            $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...
164
        }
165
        if(method_exists($this->dataProvider,'setCacheEnabled')) {
166
            $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...
167
        }
168
    }
169
170
    /**
171
     * @return string
172
     * @throws StorageException
173
     * @throws \ReflectionException
174
     */
175 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...
176
    {
177
        $dataDirectory = $this->options['dataDirectory'];
178
        if($this->options['dataDirectory'] === 'auto') {
179
            $reflection = new \ReflectionClass(AbstractData::class);
180
            $dataDirectory = sprintf('%s/var/%s', dirname($reflection->getFileName(),3), $this->options['format']);
181
        }
182
        if(is_dir($dataDirectory)){
183
            return $dataDirectory;
184
        }
185
        throw new StorageException(sprintf(StorageException::DIRECTORY_NOT_FOUND, $dataDirectory));
186
    }
187
188
    /**
189
     * @return string
190
     * @throws StorageException
191
     * @throws \ReflectionException
192
     */
193 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...
194
    {
195
        $cacheDirectory = $this->options['cacheDirectory'];
196
        if($this->options['cacheDirectory'] === 'auto') {
197
            $reflection = new \ReflectionClass(AbstractData::class);
198
            $cacheDirectory = sprintf('%s/var/cache', dirname($reflection->getFileName(),3));
199
        }
200
        if(is_dir($cacheDirectory)){
201
            return $cacheDirectory;
202
        }
203
        throw new StorageException(sprintf(StorageException::DIRECTORY_NOT_FOUND, $cacheDirectory));
204
    }
205
}