Completed
Push — master ( 415647...d02380 )
by Ricardo
02:50
created

VideoServiceMatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
    public function __construct()
29
    {
30
        $this->serviceContainer = ServicesContainerFactory::createNewServiceMatcher();
31
    }
32
33
    /**
34
     * @param string $url
35
     *
36
     * @return VideoAdapterInterface
37
     *
38
     * @throws ServiceNotAvailableException
39
     */
40
    public function parse($url)
41
    {
42
        if (isset($this->parsedUrls[$url])) {
43
            return $this->parsedUrls[$url];
44
        }
45
        /** @var array $patterns */
46
        /** @var string $serviceName */
47
        foreach ($this->getServiceContainer()->getPatterns() as $serviceName => $patterns) {
48
            /** @var string $pattern */
49
            foreach ($patterns as $pattern) {
50
                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
                    $factory = $this->getServiceContainer()->getFactory($serviceName);
52
53
                    return $this->parsedUrls[$url] = $factory($url, $pattern,
54
                        $this->getServiceContainer()->getRenderer());
55
                }
56
            }
57
        }
58
        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
            $url));
60
    }
61
62
    /**
63
     * @return ServicesContainer
64
     */
65
    public function getServiceContainer()
66
    {
67
        return $this->serviceContainer;
68
    }
69
70
    /**
71
     * @param ServicesContainer $serviceContainer
72
     */
73
    public function setServiceContainer(ServicesContainer $serviceContainer)
74
    {
75
        $this->serviceContainer = $serviceContainer;
76
    }
77
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
78