Completed
Pull Request — master (#8)
by Ricardo
15:58
created

VideoServiceMatcher::setCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace RicardoFiorani\Matcher;
4
5
use Doctrine\Common\Cache\ArrayCache;
6
use Psr\SimpleCache\CacheItemInterface;
7
use RicardoFiorani\Adapter\VideoAdapterInterface;
8
use RicardoFiorani\Container\Factory\ServicesContainerFactory;
9
use RicardoFiorani\Container\ServicesContainer;
10
use RicardoFiorani\Matcher\Exception\VideoServiceNotCompatibleException;
11
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
12
13
class VideoServiceMatcher
14
{
15
    private $serviceContainer;
16
    private $cache;
17
18 61
    public function __construct()
19
    {
20 61
        $this->serviceContainer = ServicesContainerFactory::createNewServiceMatcher();
21 61
        $this->cache = new SimpleCacheAdapter(new ArrayCache());
22 61
    }
23
24
    public function setCache(CacheItemInterface $cache)
25
    {
26
        $this->cache = $cache;
27
    }
28
29
    /**
30
     * @param string|\Psr\Http\Message\UriInterface $url
31
     * @throws VideoServiceNotCompatibleException
32
     */
33 59
    public function parse($url): VideoAdapterInterface
34
    {
35 59
        $url = (string) $url;
36 59
        $cacheKey = hash('md5', $url);
37 59
        if ($this->cache->has($cacheKey)) {
38 6
            return $this->cache->get($cacheKey);
39
        }
40
41
        /** @var array $patterns */
42
        /** @var string $serviceName */
43 59
        foreach ($this->getServiceContainer()->getPatterns() as $serviceName => $patterns) {
44
            /** @var string $pattern */
45 59
            foreach ($patterns as $pattern) {
46 59
                if (false != preg_match($pattern, $url)) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match($pattern, $url) of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
47 56
                    $factory = $this->getServiceContainer()->getFactory($serviceName);
48 56
                    $renderer = $this->getServiceContainer()->getRenderer();
49 56
                    $adapter = $factory($url, $pattern, $renderer);
50 56
                    $this->cache->set($cacheKey, $adapter);
51
52 59
                    return $adapter;
53
                }
54
            }
55
        }
56
57 3
        throw new VideoServiceNotCompatibleException(
58 3
            sprintf('The url "%s" could not be parsed by any of the services available.', $url)
59
        );
60
    }
61
62 61
    public function getServiceContainer(): ServicesContainer
63
    {
64 61
        return $this->serviceContainer;
65
    }
66
67 1
    public function setServiceContainer(ServicesContainer $serviceContainer)
68
    {
69 1
        $this->serviceContainer = $serviceContainer;
70 1
    }
71
}
72