Completed
Push — 4.0 ( c6bc6b...71d650 )
by Serhii
02:10
created

AbstractDetection::setAttributes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
4
namespace EndorphinStudio\Detector\Detection;
5
6
7
use EndorphinStudio\Detector\Data\AbstractData;
8
use EndorphinStudio\Detector\Detector;
9
use EndorphinStudio\Detector\Tools;
10
11
abstract class AbstractDetection implements DetectionInterface
12
{
13
    /** @var array */
14
    protected $config;
15
    /** @var Detector */
16
    protected $detector;
17
18
    /** @var AbstractData */
19
    protected $resultObject;
20
21
    public function init(Detector $detector)
22
    {
23
        $this->detector = $detector;
24
        echo static::class . PHP_EOL;
25
    }
26
27
    protected function initResultObject()
28
    {
29
        // init default value from data
30
        foreach ($this->config['default'] as $defaultKey => $defaultValue) {
31
            Tools::runSetter($this->resultObject, $defaultKey, $defaultValue);
32
        }
33
    }
34
35
    public abstract function detect(string $ua);
36
37
    protected function detectByPattern(array $patternList)
38
    {
39
        foreach ($patternList as $patternId => $patternData) {
40
            $pattern = $this->getPattern($patternId, $patternData);
41
42
            if (preg_match($pattern['pattern'], $this->detector->getUserAgent())) {
43
                return ['name' => $patternId, 'version' => Tools::getVersion($pattern['version'], $this->detector->getUserAgent()), 'originalInfo' => $patternData];
44
            }
45
        }
46
        return null;
47
    }
48
49
    private function getPattern(string $patternId, array $patternData): array
50
    {
51
        if (array_key_exists('default', $patternData) && $patternData['default'] === true) {
52
            return ['pattern' => sprintf('/%s/', $patternId), 'version' => $patternId];
53
        }
54
        return ['pattern' => sprintf('/%s/', $patternData['pattern']), 'version' => array_key_exists('version', $patternData) ? $patternData['version'] : $patternId];
55
    }
56
57
    protected function setAttributes($info)
58
    {
59
        $result = $this->detector->getResultObject();
60
        if (array_key_exists('attributes', $info)) {
61
            foreach ($info['attributes'] as $attributeKey => $attributeValue) {
62
                Tools::runSetter($result, $attributeKey, $attributeValue);
63
            }
64
        }
65
    }
66
67 View Code Duplication
    protected function detectByType(): array
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...
68
    {
69
        foreach ($this->config as $type => $patternList) {
70
            if ($type === 'default') {
71
                continue;
72
            }
73
            $browser = $this->detectByPattern($patternList);
74
            if ($browser) {
75
                return array_merge($browser, ['type' => $type]);
76
            }
77
        }
78
        return [];
79
    }
80
}