Completed
Push — master ( 765aa7...0148b2 )
by Pavel
02:44
created

ContainerLinkResolver::supports()   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 1
crap 1
1
<?php
2
3
namespace Bankiru\Seo\Resolver;
4
5
use Bankiru\Seo\Entity\ContainerLinkInterface;
6
use Bankiru\Seo\Exception\LinkResolutionException;
7
8
final class ContainerLinkResolver implements LinkResolver
9
{
10
    /** @var LinkResolver[] */
11
    private $resolvers = [];
12
13
    /**
14
     * ContainerLinkResolver constructor.
15
     *
16
     * @param LinkResolver[] $resolvers
17
     */
18 1
    public function __construct(array $resolvers = [])
19
    {
20 1
        $this->resolvers   = $resolvers;
21 1
        $this->resolvers[] = $this;
22 1
    }
23
24 1
    public function addResolver(LinkResolver $resolver)
25
    {
26 1
        $this->resolvers[] = $resolver;
27 1
    }
28
29
    /** {@inheritdoc} */
30 1
    public function resolve($container)
31
    {
32 1
        if (!$this->supports($container)) {
33 1
            throw new LinkResolutionException('Link type not supported');
34
        }
35
36
        /** @var ContainerLinkInterface $container */
37 1
        $result = [];
38 1
        foreach ($container as $link) {
39 1
            $result = array_merge($result, $this->matchResolver($link)->resolve($link));
40 1
        }
41
42 1
        return $result;
43
    }
44
45
    /** {@inheritdoc} */
46 1
    public function supports($link)
47
    {
48 1
        return $link instanceof ContainerLinkInterface;
49
    }
50
51
    /**
52
     * @param $link
53
     *
54
     * @return LinkResolver
55
     * @throws LinkResolutionException
56
     */
57 1
    private function matchResolver($link)
58
    {
59 1
        foreach ($this->resolvers as $resolver) {
60 1
            if ($resolver->supports($link)) {
61 1
                return $resolver;
62
            }
63 1
        }
64 1
        throw new LinkResolutionException('Cannot find link resolver for link: '.get_class($link));
65
    }
66
}
67