Completed
Push — master ( 7014bb...a38bcf )
by James
15s
created

Browscap::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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 7
    public function __construct(CacheInterface $cache, LoggerInterface $logger)
51
    {
52 7
        $this->cache = new BrowscapCache($cache, $logger);
53 7
        $this->logger = $logger;
54 7
    }
55
56
    /**
57
     * Set theformatter instance to use for the getBrowser() result
58
     *
59
     * @param \BrowscapPHP\Formatter\FormatterInterface $formatter
60
     */
61 5
    public function setFormatter(Formatter\FormatterInterface $formatter) : void
62
    {
63 5
        $this->formatter = $formatter;
64 5
    }
65
66
    /**
67
     * @return \BrowscapPHP\Formatter\FormatterInterface
68
     */
69 3
    public function getFormatter() : FormatterInterface
70
    {
71 3
        if (null === $this->formatter) {
72 1
            $this->setFormatter(new Formatter\PhpGetBrowser());
73
        }
74
75 3
        return $this->formatter;
76
    }
77
78
    /**
79
     * Sets the parser instance to use
80
     *
81
     * @param \BrowscapPHP\Parser\ParserInterface $parser
82
     */
83 4
    public function setParser(ParserInterface $parser) : void
84
    {
85 4
        $this->parser = $parser;
86 4
    }
87
88
    /**
89
     * returns an instance of the used parser class
90
     *
91
     * @return \BrowscapPHP\Parser\ParserInterface
92
     */
93 5
    public function getParser() : ParserInterface
94
    {
95 5
        if (null === $this->parser) {
96 1
            $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 1
            $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 1
            $this->parser = new Parser\Ini($patternHelper, $dataHelper, $this->getFormatter());
100
        }
101
102 5
        return $this->parser;
103
    }
104
105
    /**
106
     * parses the given user agent to get the information about the browser
107
     *
108
     * if no user agent is given, it uses {@see \BrowscapPHP\Helper\Support} to get it
109
     *
110
     * @param string $userAgent the user agent string
111
     *
112
     * @throws \BrowscapPHP\Exception
113
     * @return \stdClass              the object containing the browsers details. Array if
114
     *                                $return_array is set to true.
115
     */
116 4
    public function getBrowser(string $userAgent = null) : \stdClass
117
    {
118 4
        if (null === $this->cache->getVersion()) {
119
            // there is no active/warm cache available
120 1
            throw new Exception('there is no active cache available, please run the update command');
121
        }
122
123
        // Automatically detect the useragent
124 3
        if (! is_string($userAgent)) {
125 1
            $support = new Helper\Support($_SERVER);
126 1
            $userAgent = $support->getUserAgent();
127
        }
128
129
        // try to get browser data
130 3
        $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 3
        if ($formatter === null) {
135 1
            $formatter = $this->getFormatter();
136
        }
137
138 3
        return $formatter->getData();
139
    }
140
}
141