Completed
Pull Request — master (#186)
by Jay
05:38
created

Browscap::setCache()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 8
cts 9
cp 0.8889
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
crap 3.0123
1
<?php
2
/**
3
 * Copyright (c) 1998-2015 Browser Capabilities Project
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included
13
 * in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category   Browscap-PHP
24
 * @copyright  1998-2015 Browser Capabilities Project
25
 * @license    http://www.opensource.org/licenses/MIT MIT License
26
 * @link       https://github.com/browscap/browscap-php/
27
 */
28
29
namespace BrowscapPHP;
30
31
use BrowscapPHP\Cache\BrowscapCache;
32
use BrowscapPHP\Cache\BrowscapCacheInterface;
33
use BrowscapPHP\Helper\Quoter;
34
use BrowscapPHP\Parser\ParserInterface;
35
use Psr\Log\LoggerInterface;
36
use Psr\Log\NullLogger;
37
use WurflCache\Adapter\AdapterInterface;
38
use WurflCache\Adapter\File;
39
40
/**
41
 * Browscap.ini parsing class with caching and update capabilities
42
 *
43
 * @category   Browscap-PHP
44
 * @author     Jonathan Stoppani <[email protected]>
45
 * @author     Vítor Brandão <[email protected]>
46
 * @author     Mikołaj Misiurewicz <[email protected]>
47
 * @author     Christoph Ziegenberg <[email protected]>
48
 * @author     Thomas Müller <[email protected]>
49
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
50
 * @version    3.0
51
 * @license    http://www.opensource.org/licenses/MIT MIT License
52
 * @link       https://github.com/browscap/browscap-php/
53
 */
54
class Browscap
55
{
56
    /**
57
     * Parser to use
58
     *
59
     * @var \BrowscapPHP\Parser\ParserInterface
60
     */
61
    private $parser = null;
62
63
    /**
64
     * Formatter to use
65
     *
66
     * @var \BrowscapPHP\Formatter\FormatterInterface
67
     */
68
    private $formatter = null;
69
70
    /**
71
     * The cache instance
72
     *
73
     * @var \BrowscapPHP\Cache\BrowscapCacheInterface
74
     */
75
    private $cache = null;
76
77
    /**
78
     * @var @var \Psr\Log\LoggerInterface
79
     */
80
    private $logger = null;
81
82
    /**
83
     * Set theformatter instance to use for the getBrowser() result
84
     *
85
     * @param \BrowscapPHP\Formatter\FormatterInterface $formatter
86
     *
87
     * @return \BrowscapPHP\Browscap
88
     */
89 6
    public function setFormatter(Formatter\FormatterInterface $formatter)
90
    {
91 6
        $this->formatter = $formatter;
92
93 6
        return $this;
94
    }
95
96
    /**
97
     * @return \BrowscapPHP\Formatter\FormatterInterface
98
     */
99 4
    public function getFormatter()
100
    {
101 4
        if (null === $this->formatter) {
102 2
            $this->setFormatter(new Formatter\PhpGetBrowser());
103
        }
104
105 4
        return $this->formatter;
106
    }
107
108
    /**
109
     * Gets a cache instance
110
     *
111
     * @return \BrowscapPHP\Cache\BrowscapCacheInterface
112
     */
113 9 View Code Duplication
    public function getCache()
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...
114
    {
115 9
        if (null === $this->cache) {
116 3
            $cacheDirectory = __DIR__ . '/../resources/';
117
118 3
            $cacheAdapter = new File(
119 3
                [File::DIR => $cacheDirectory]
120
            );
121
122 3
            $this->cache = new BrowscapCache($cacheAdapter);
123
        }
124
125 9
        return $this->cache;
126
    }
127
128
    /**
129
     * Sets a cache instance
130
     *
131
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface|\WurflCache\Adapter\AdapterInterface $cache
132
     *
133
     * @throws \BrowscapPHP\Exception
134
     * @return \BrowscapPHP\Browscap
135
     */
136 7 View Code Duplication
    public function setCache($cache)
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...
137
    {
138 7
        if ($cache instanceof BrowscapCacheInterface) {
139 5
            $this->cache = $cache;
140
        } elseif ($cache instanceof AdapterInterface) {
141 1
            $this->cache = new BrowscapCache($cache);
142
        } else {
143 1
            throw new Exception(
144
                'the cache has to be an instance of \BrowscapPHP\Cache\BrowscapCacheInterface or '
145 1
                . 'an instanceof of \WurflCache\Adapter\AdapterInterface',
146 1
                Exception::CACHE_INCOMPATIBLE
147
            );
148
        }
149
150 6
        return $this;
151
    }
152
153
    /**
154
     * Sets the parser instance to use
155
     *
156
     * @param \BrowscapPHP\Parser\ParserInterface $parser
157
     *
158
     * @return \BrowscapPHP\Browscap
159
     */
160 4
    public function setParser(ParserInterface $parser)
161
    {
162 4
        $this->parser = $parser;
163
164 4
        return $this;
165
    }
166
167
    /**
168
     * returns an instance of the used parser class
169
     *
170
     * @return \BrowscapPHP\Parser\ParserInterface
171
     */
172 6
    public function getParser()
173
    {
174 6
        if (null === $this->parser) {
175 2
            $cache  = $this->getCache();
176 2
            $logger = $this->getLogger();
177 2
            $quoter = new Quoter();
178
179 2
            $patternHelper = new Parser\Helper\GetPattern($cache, $logger);
180 2
            $dataHelper    = new Parser\Helper\GetData($cache, $logger, $quoter);
181
182 2
            $this->parser = new Parser\Ini($patternHelper, $dataHelper, $this->getFormatter());
183
        }
184
185 6
        return $this->parser;
186
    }
187
188
    /**
189
     * Sets a logger instance
190
     *
191
     * @param \Psr\Log\LoggerInterface $logger
192
     *
193
     * @return \BrowscapPHP\Browscap
194
     */
195 2
    public function setLogger(LoggerInterface $logger)
196
    {
197 2
        $this->logger = $logger;
198
199 2
        return $this;
200
    }
201
202
    /**
203
     * returns a logger instance
204
     *
205
     * @return \Psr\Log\LoggerInterface
206
     */
207 4
    public function getLogger()
208
    {
209 4
        if (null === $this->logger) {
210 2
            $this->logger = new NullLogger();
211
        }
212
213 4
        return $this->logger;
214
    }
215
216
    /**
217
     * parses the given user agent to get the information about the browser
218
     *
219
     * if no user agent is given, it uses {@see \BrowscapPHP\Helper\Support} to get it
220
     *
221
     * @param string $userAgent the user agent string
222
     *
223
     * @throws \BrowscapPHP\Exception
224
     * @return \stdClass              the object containing the browsers details. Array if
225
     *                                $return_array is set to true.
226
     */
227 5
    public function getBrowser($userAgent = null)
228
    {
229 5
        if (null === $this->getCache()->getVersion()) {
230
            // there is no active/warm cache available
231 1
            throw new Exception('there is no active cache available, please run the update command');
232
        }
233
234
        // Automatically detect the useragent
235 4
        if (!isset($userAgent)) {
236 2
            $support   = new Helper\Support($_SERVER);
237 2
            $userAgent = $support->getUserAgent();
238
        }
239
240
        // try to get browser data
241 4
        $formatter = $this->getParser()->getBrowser($userAgent);
242
243
        // if return is still NULL, updates are disabled... in this
244
        // case we return an empty formatter instance
245 4
        if ($formatter === null) {
246 2
            return $this->getFormatter()->getData();
247
        }
248
249 2
        return $formatter->getData();
250
    }
251
}
252