Completed
Push — master ( 41d47b...c28fef )
by James
10s
created

Browscap   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 13
dl 0
loc 198
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setFormatter() 0 6 1
A getFormatter() 0 8 2
A getCache() 0 14 2
A setParser() 0 6 1
A getParser() 0 15 2
A setLogger() 0 6 1
A getLogger() 0 8 2
B getBrowser() 0 24 4
A setCache() 0 16 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP;
5
6
use BrowscapPHP\Cache\BrowscapCache;
7
use BrowscapPHP\Cache\BrowscapCacheInterface;
8
use BrowscapPHP\Formatter\FormatterInterface;
9
use BrowscapPHP\Helper\Quoter;
10
use BrowscapPHP\Parser\ParserInterface;
11
use Psr\Log\LoggerInterface;
12
use Psr\Log\NullLogger;
13
use WurflCache\Adapter\AdapterInterface;
14
use WurflCache\Adapter\File;
15
16
/**
17
 * Browscap.ini parsing class with caching and update capabilities
18
 */
19
final class Browscap
20
{
21
    /**
22
     * Parser to use
23
     *
24
     * @var \BrowscapPHP\Parser\ParserInterface|null
25
     */
26
    private $parser;
27
28
    /**
29
     * Formatter to use
30
     *
31
     * @var \BrowscapPHP\Formatter\FormatterInterface|null
32
     */
33
    private $formatter;
34
35
    /**
36
     * The cache instance
37
     *
38
     * @var \BrowscapPHP\Cache\BrowscapCacheInterface|null
39
     */
40
    private $cache;
41
42
    /**
43
     * @var \Psr\Log\LoggerInterface|null
44
     */
45
    private $logger;
46
47
    /**
48
     * Set theformatter instance to use for the getBrowser() result
49
     *
50
     * @param \BrowscapPHP\Formatter\FormatterInterface $formatter
51
     *
52
     * @return \BrowscapPHP\Browscap
53
     */
54 6
    public function setFormatter(Formatter\FormatterInterface $formatter) : self
55
    {
56 6
        $this->formatter = $formatter;
57
58 6
        return $this;
59
    }
60
61
    /**
62
     * @return \BrowscapPHP\Formatter\FormatterInterface
63
     */
64 4
    public function getFormatter() : FormatterInterface
65
    {
66 4
        if (null === $this->formatter) {
67 2
            $this->setFormatter(new Formatter\PhpGetBrowser());
68
        }
69
70 4
        return $this->formatter;
71
    }
72
73
    /**
74
     * Gets a cache instance
75
     *
76
     * @return \BrowscapPHP\Cache\BrowscapCacheInterface
77
     */
78 9
    public function getCache() : BrowscapCacheInterface
79
    {
80 9
        if (null === $this->cache) {
81 3
            $cacheDirectory = __DIR__ . '/../resources/';
82
83 3
            $cacheAdapter = new File(
84 3
                [File::DIR => $cacheDirectory]
85
            );
86
87 3
            $this->cache = new BrowscapCache($cacheAdapter);
88
        }
89
90 9
        return $this->cache;
91
    }
92
93
    /**
94
     * Sets a cache instance
95
     *
96
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface|\WurflCache\Adapter\AdapterInterface $cache
97
     *
98
     * @throws \BrowscapPHP\Exception
99
     * @return \BrowscapPHP\Browscap
100
     */
101 7
    public function setCache($cache) : self
102
    {
103 7
        if ($cache instanceof BrowscapCacheInterface) {
104 5
            $this->cache = $cache;
105 2
        } elseif ($cache instanceof AdapterInterface) {
106 1
            $this->cache = new BrowscapCache($cache);
107
        } else {
108 1
            throw new Exception(
109
                'the cache has to be an instance of \BrowscapPHP\Cache\BrowscapCacheInterface or '
110 1
                . 'an instanceof of \WurflCache\Adapter\AdapterInterface',
111 1
                Exception::CACHE_INCOMPATIBLE
112
            );
113
        }
114
115 6
        return $this;
116
    }
117
118
    /**
119
     * Sets the parser instance to use
120
     *
121
     * @param \BrowscapPHP\Parser\ParserInterface $parser
122
     *
123
     * @return \BrowscapPHP\Browscap
124
     */
125 4
    public function setParser(ParserInterface $parser) : self
126
    {
127 4
        $this->parser = $parser;
128
129 4
        return $this;
130
    }
131
132
    /**
133
     * returns an instance of the used parser class
134
     *
135
     * @return \BrowscapPHP\Parser\ParserInterface
136
     */
137 6
    public function getParser() : ParserInterface
138
    {
139 6
        if (null === $this->parser) {
140 2
            $cache = $this->getCache();
141 2
            $logger = $this->getLogger();
142 2
            $quoter = new Quoter();
143
144 2
            $patternHelper = new Parser\Helper\GetPattern($cache, $logger);
145 2
            $dataHelper = new Parser\Helper\GetData($cache, $logger, $quoter);
146
147 2
            $this->parser = new Parser\Ini($patternHelper, $dataHelper, $this->getFormatter());
148
        }
149
150 6
        return $this->parser;
151
    }
152
153
    /**
154
     * Sets a logger instance
155
     *
156
     * @param \Psr\Log\LoggerInterface $logger
157
     *
158
     * @return \BrowscapPHP\Browscap
159
     */
160 2
    public function setLogger(LoggerInterface $logger) : self
161
    {
162 2
        $this->logger = $logger;
163
164 2
        return $this;
165
    }
166
167
    /**
168
     * returns a logger instance
169
     *
170
     * @return \Psr\Log\LoggerInterface
171
     */
172 4
    public function getLogger() : LoggerInterface
173
    {
174 4
        if (null === $this->logger) {
175 2
            $this->logger = new NullLogger();
176
        }
177
178 4
        return $this->logger;
179
    }
180
181
    /**
182
     * parses the given user agent to get the information about the browser
183
     *
184
     * if no user agent is given, it uses {@see \BrowscapPHP\Helper\Support} to get it
185
     *
186
     * @param string $userAgent the user agent string
187
     *
188
     * @throws \BrowscapPHP\Exception
189
     * @return \stdClass              the object containing the browsers details. Array if
190
     *                                $return_array is set to true.
191
     */
192 5
    public function getBrowser(string $userAgent = null) : ?\stdClass
193
    {
194 5
        if (null === $this->getCache()->getVersion()) {
195
            // there is no active/warm cache available
196 1
            throw new Exception('there is no active cache available, please run the update command');
197
        }
198
199
        // Automatically detect the useragent
200 4
        if (! isset($userAgent)) {
201 2
            $support = new Helper\Support($_SERVER);
202 2
            $userAgent = $support->getUserAgent();
203
        }
204
205
        // try to get browser data
206 4
        $formatter = $this->getParser()->getBrowser($userAgent);
207
208
        // if return is still NULL, updates are disabled... in this
209
        // case we return an empty formatter instance
210 4
        if ($formatter === null) {
211 2
            return $this->getFormatter()->getData();
212
        }
213
214 2
        return $formatter->getData();
215
    }
216
}
217