Completed
Push — master ( cb16b1...c87003 )
by Ricardo
04:25 queued 25s
created

VideoServiceMatcher::getServiceContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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\Exception\ServiceNotAvailableException;
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 60
    public function __construct()
29
    {
30 60
        $this->serviceContainer = ServicesContainerFactory::createNewServiceMatcher();
31 60
    }
32
33
    /**
34
     * @param string $url
35
     *
36
     * @return VideoAdapterInterface
37
     *
38
     * @throws ServiceNotAvailableException
39
     */
40 58
    public function parse($url)
41
    {
42 58
        if (isset($this->parsedUrls[$url])) {
43 6
            return $this->parsedUrls[$url];
44
        }
45
        /** @var array $patterns */
46
        /** @var string $serviceName */
47 58
        foreach ($this->getServiceContainer()->getPatterns() as $serviceName => $patterns) {
48
            /** @var string $pattern */
49 58
            foreach ($patterns as $pattern) {
50 58
                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...
51 55
                    $factory = $this->getServiceContainer()->getFactory($serviceName);
52
53 55
                    return $this->parsedUrls[$url] = $factory($url, $pattern,
54 55
                        $this->getServiceContainer()->getRenderer());
55
                }
56 46
            }
57 46
        }
58 3
        throw new ServiceNotAvailableException(sprintf('The url "%s" could not be parsed by any of the services available.',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
59 3
            $url));
60
    }
61
62
    /**
63
     * @return ServicesContainer
64
     */
65 60
    public function getServiceContainer()
66
    {
67 60
        return $this->serviceContainer;
68
    }
69
70
    /**
71
     * @param ServicesContainer $serviceContainer
72
     */
73 1
    public function setServiceContainer(ServicesContainer $serviceContainer)
74
    {
75 1
        $this->serviceContainer = $serviceContainer;
76 1
    }
77
}
78