Completed
Pull Request — master (#18)
by Ricardo
05:45
created

VideoServiceMatcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 68
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 24 5
A getServiceContainer() 0 4 1
A setServiceContainer() 0 4 1
1
<?php
2
3
namespace RicardoFiorani\Matcher;
4
5
use RicardoFiorani\Adapter\VideoAdapterInterface;
6
use RicardoFiorani\Container\Factory\ServicesContainerFactory;
7
use RicardoFiorani\Container\ServicesContainer;
8
use RicardoFiorani\Matcher\Exception\VideoServiceNotCompatibleException;
9
10
/**
11
 * @author Ricardo Fiorani
12
 */
13
class VideoServiceMatcher
14
{
15
    /**
16
     * @var ServicesContainer
17
     */
18
    private $serviceContainer;
19
20
    /**
21
     * @var array
22
     */
23
    private $parsedUrls = array();
24
25
    /**
26
     * VideoServiceMatcher constructor.
27
     */
28
    public function __construct()
29
    {
30
        $this->serviceContainer = ServicesContainerFactory::createNewServiceMatcher();
31
    }
32
33
    /**
34
     * @param string $url
35
     *
36
     * @return VideoAdapterInterface
37
     *
38
     * @throws VideoServiceNotCompatibleException
39
     */
40
    public function parse($url)
41
    {
42
        if (isset($this->parsedUrls[$url])) {
43
            return $this->parsedUrls[$url];
44
        }
45
46
        /** @var array $patterns */
47
        /** @var string $serviceName */
48
        foreach ($this->getServiceContainer()->getPatterns() as $serviceName => $patterns) {
49
            /** @var string $pattern */
50
            foreach ($patterns as $pattern) {
51
                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...
52
                    $factory = $this->getServiceContainer()->getFactory($serviceName);
53
54
                    return $this->parsedUrls[$url] = $factory($url, $pattern,
55
                        $this->getServiceContainer()->getRenderer());
56
                }
57
            }
58
        }
59
60
        throw new VideoServiceNotCompatibleException(
61
            sprintf('The url "%s" could not be parsed by any of the services available.', $url)
62
        );
63
    }
64
65
    /**
66
     * @return ServicesContainer
67
     */
68
    public function getServiceContainer()
69
    {
70
        return $this->serviceContainer;
71
    }
72
73
    /**
74
     * @param ServicesContainer $serviceContainer
75
     */
76
    public function setServiceContainer(ServicesContainer $serviceContainer)
77
    {
78
        $this->serviceContainer = $serviceContainer;
79
    }
80
}
81