Browscap   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 11
dl 0
loc 120
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setFormatter() 0 4 1
A setParser() 0 4 1
A getParser() 0 11 2
A getBrowser() 0 29 5
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP;
5
6
use BrowscapPHP\Cache\BrowscapCache;
7
use BrowscapPHP\Helper\Quoter;
8
use BrowscapPHP\Parser\ParserInterface;
9
use Psr\Log\LoggerInterface;
10
use Psr\SimpleCache\CacheInterface;
11
12
/**
13
 * Browscap.ini parsing class with caching and update capabilities
14
 */
15
final class Browscap implements BrowscapInterface
16
{
17
    /**
18
     * Parser to use
19
     *
20
     * @var \BrowscapPHP\Parser\ParserInterface|null
21
     */
22
    private $parser;
23
24
    /**
25
     * Formatter to use
26
     *
27
     * @var \BrowscapPHP\Formatter\FormatterInterface
28
     */
29
    private $formatter;
30
31
    /**
32
     * The cache instance
33
     *
34
     * @var \BrowscapPHP\Cache\BrowscapCacheInterface
35
     */
36
    private $cache;
37
38
    /**
39
     * @var \Psr\Log\LoggerInterface
40
     */
41
    private $logger;
42
43
    /**
44
     * Browscap constructor.
45
     *
46
     * @param \Psr\SimpleCache\CacheInterface  $cache
47
     * @param LoggerInterface $logger
48
     */
49 7
    public function __construct(CacheInterface $cache, LoggerInterface $logger)
50
    {
51 7
        $this->cache = new BrowscapCache($cache, $logger);
52 7
        $this->logger = $logger;
53
54 7
        $this->formatter = new Formatter\PhpGetBrowser();
55 7
    }
56
57
    /**
58
     * Set theformatter instance to use for the getBrowser() result
59
     *
60
     * @param \BrowscapPHP\Formatter\FormatterInterface $formatter
61
     */
62 4
    public function setFormatter(Formatter\FormatterInterface $formatter) : void
63
    {
64 4
        $this->formatter = $formatter;
65 4
    }
66
67
    /**
68
     * Sets the parser instance to use
69
     *
70
     * @param \BrowscapPHP\Parser\ParserInterface $parser
71
     */
72 4
    public function setParser(ParserInterface $parser) : void
73
    {
74 4
        $this->parser = $parser;
75 4
    }
76
77
    /**
78
     * returns an instance of the used parser class
79
     *
80
     * @return \BrowscapPHP\Parser\ParserInterface
81
     */
82 5
    public function getParser() : ParserInterface
83
    {
84 5
        if (null === $this->parser) {
85 1
            $patternHelper = new Parser\Helper\GetPattern($this->cache, $this->logger);
86 1
            $dataHelper = new Parser\Helper\GetData($this->cache, $this->logger, new Quoter());
87
88 1
            $this->parser = new Parser\Ini($patternHelper, $dataHelper, $this->formatter);
89
        }
90
91 5
        return $this->parser;
92
    }
93
94
    /**
95
     * parses the given user agent to get the information about the browser
96
     *
97
     * if no user agent is given, it uses {@see \BrowscapPHP\Helper\Support} to get it
98
     *
99
     * @param string $userAgent the user agent string
100
     *
101
     * @throws \BrowscapPHP\Exception
102
     *
103
     * @return \stdClass              the object containing the browsers details.
104
     */
105
    public function getBrowser(?string $userAgent = null) : \stdClass
106 4
    {
107
        if (null === $this->cache->getVersion()) {
108 4
            // there is no active/warm cache available
109
            throw new Exception('there is no active cache available, please use the BrowscapUpdater and run the update command');
110 1
        }
111
112
        // Automatically detect the useragent
113
        if (! is_string($userAgent)) {
114 3
            $support = new Helper\Support($_SERVER);
115 1
            $userAgent = $support->getUserAgent();
116 1
        }
117
118
        try {
119
            // try to get browser data
120 3
            $formatter = $this->getParser()->getBrowser($userAgent);
121
        } catch (\UnexpectedValueException $e) {
122
            $this->logger->error(sprintf('could not parse useragent "%s"', $userAgent));
123
            $formatter = null;
124 3
        }
125 1
126
        // if return is still NULL, updates are disabled... in this
127
        // case we return an empty formatter instance
128 3
        if (null === $formatter) {
129
            $formatter = $this->formatter;
130
        }
131
132
        return $formatter->getData();
133
    }
134
}
135