Completed
Pull Request — master (#218)
by Thomas
48:30 queued 13:26
created

Browscap   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setFormatter() 0 4 1
A getFormatter() 0 8 2
A setParser() 0 4 1
A getParser() 0 11 2
B getBrowser() 0 24 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP;
5
6
use BrowscapPHP\Cache\BrowscapCache;
7
use BrowscapPHP\Formatter\FormatterInterface;
8
use BrowscapPHP\Helper\Quoter;
9
use BrowscapPHP\Parser\ParserInterface;
10
use Psr\Log\LoggerInterface;
11
use Psr\SimpleCache\CacheInterface;
12
13
/**
14
 * Browscap.ini parsing class with caching and update capabilities
15
 */
16
final class Browscap implements BrowscapInterface
17
{
18
    /**
19
     * Parser to use
20
     *
21
     * @var \BrowscapPHP\Parser\ParserInterface|null
22
     */
23
    private $parser;
24
25
    /**
26
     * Formatter to use
27
     *
28
     * @var \BrowscapPHP\Formatter\FormatterInterface|null
29
     */
30
    private $formatter;
31
32
    /**
33
     * The cache instance
34
     *
35
     * @var \BrowscapPHP\Cache\BrowscapCacheInterface
36
     */
37
    private $cache;
38
39
    /**
40
     * @var \Psr\Log\LoggerInterface|null
41
     */
42
    private $logger;
43
44
    /**
45
     * Browscap constructor.
46
     *
47
     * @param \Psr\SimpleCache\CacheInterface  $cache
48
     * @param LoggerInterface $logger
49
     */
50
    public function __construct(CacheInterface $cache, LoggerInterface $logger)
51
    {
52
        $this->cache = new BrowscapCache($cache, $logger);
53
        $this->logger = $logger;
54 6
    }
55
56 6
    /**
57
     * Set theformatter instance to use for the getBrowser() result
58 6
     *
59
     * @param \BrowscapPHP\Formatter\FormatterInterface $formatter
60
     */
61
    public function setFormatter(Formatter\FormatterInterface $formatter) : void
62
    {
63
        $this->formatter = $formatter;
64 4
    }
65
66 4
    /**
67 2
     * @return \BrowscapPHP\Formatter\FormatterInterface
68
     */
69
    public function getFormatter() : FormatterInterface
70 4
    {
71
        if (null === $this->formatter) {
72
            $this->setFormatter(new Formatter\PhpGetBrowser());
73
        }
74
75
        return $this->formatter;
76
    }
77
78 9
    /**
79
     * Sets the parser instance to use
80 9
     *
81 3
     * @param \BrowscapPHP\Parser\ParserInterface $parser
82
     */
83 3
    public function setParser(ParserInterface $parser) : void
84 3
    {
85
        $this->parser = $parser;
86
    }
87 3
88
    /**
89
     * returns an instance of the used parser class
90 9
     *
91
     * @return \BrowscapPHP\Parser\ParserInterface
92
     */
93
    public function getParser() : ParserInterface
94
    {
95
        if (null === $this->parser) {
96
            $patternHelper = new Parser\Helper\GetPattern($this->cache, $this->logger);
0 ignored issues
show
Bug introduced by
It seems like $this->logger can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
97
            $dataHelper = new Parser\Helper\GetData($this->cache, $this->logger, new Quoter());
0 ignored issues
show
Bug introduced by
It seems like $this->logger can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
98
99
            $this->parser = new Parser\Ini($patternHelper, $dataHelper, $this->getFormatter());
100
        }
101 7
102
        return $this->parser;
103 7
    }
104 5
105 2
    /**
106 1
     * parses the given user agent to get the information about the browser
107
     *
108 1
     * if no user agent is given, it uses {@see \BrowscapPHP\Helper\Support} to get it
109
     *
110 1
     * @param string $userAgent the user agent string
111 1
     *
112
     * @throws \BrowscapPHP\Exception
113
     * @return \stdClass              the object containing the browsers details. Array if
114
     *                                $return_array is set to true.
115 6
     */
116
    public function getBrowser(string $userAgent = null) : \stdClass
117
    {
118
        if (null === $this->cache->getVersion()) {
119
            // there is no active/warm cache available
120
            throw new Exception('there is no active cache available, please run the update command');
121
        }
122
123
        // Automatically detect the useragent
124
        if (! is_string($userAgent)) {
125 4
            $support = new Helper\Support($_SERVER);
126
            $userAgent = $support->getUserAgent();
127 4
        }
128
129 4
        // try to get browser data
130
        $formatter = $this->getParser()->getBrowser($userAgent);
131
132
        // if return is still NULL, updates are disabled... in this
133
        // case we return an empty formatter instance
134
        if ($formatter === null) {
135
            $formatter = $this->getFormatter();
136
        }
137 6
138
        return $formatter->getData();
139 6
    }
140
}
141