Completed
Push — master ( 301f3c...656cd4 )
by James
03:43
created

Browscap::getFormatter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Browscap::setParser() 0 4 1
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. Array if
104
     *                                $return_array is set to true.
105
     */
106 4
    public function getBrowser(string $userAgent = null) : \stdClass
107
    {
108 4
        if (null === $this->cache->getVersion()) {
109
            // there is no active/warm cache available
110 1
            throw new Exception('there is no active cache available, please use the BrowscapUpdater and run the update command');
111
        }
112
113
        // Automatically detect the useragent
114 3
        if (! is_string($userAgent)) {
115 1
            $support = new Helper\Support($_SERVER);
116 1
            $userAgent = $support->getUserAgent();
117
        }
118
119
        // try to get browser data
120 3
        $formatter = $this->getParser()->getBrowser($userAgent);
121
122
        // if return is still NULL, updates are disabled... in this
123
        // case we return an empty formatter instance
124 3
        if (null === $formatter) {
125 1
            $formatter = $this->formatter;
126
        }
127
128 3
        return $formatter->getData();
129
    }
130
}
131