CachedParser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dziki\MonologSentryBundle\UserAgent;
6
7
use Psr\SimpleCache\CacheInterface;
8
9
class CachedParser implements ParserInterface
10
{
11
    /**
12
     * @var CacheInterface
13
     */
14
    private $cache;
15
    /**
16
     * @var ParserInterface
17
     */
18
    private $parser;
19
20
    public function __construct(CacheInterface $cache, ParserInterface $parser)
21
    {
22
        $this->cache = $cache;
23
        $this->parser = $parser;
24
    }
25
26
    public function parse(string $userAgent): UserAgent
27
    {
28
        $userAgentHash = md5($userAgent);
29
30
        if ($serializedUserAgent = $this->cache->get($userAgentHash)) {
31
            return unserialize($serializedUserAgent, ['allowed_classes' => [UserAgent::class]]);
32
        }
33
34
        $parsedUserAgent = $this->parser->parse($userAgent);
35
        $this->cache->set($userAgentHash, serialize($parsedUserAgent));
36
37
        return $parsedUserAgent;
38
    }
39
}
40