Completed
Push — master ( 90ccc1...22512f )
by Kamil
35:43
created

CompositeMetadataHierarchyProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 6
rs 9.4285
c 2
b 0
f 1
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Metadata\HierarchyProvider;
13
14
use Sylius\Component\Metadata\Model\MetadataSubjectInterface;
15
16
/**
17
 * @author Kamil Kokot <[email protected]>
18
 */
19
final class CompositeMetadataHierarchyProvider implements MetadataHierarchyProviderInterface
20
{
21
    /**
22
     * @var MetadataHierarchyProviderInterface[]
23
     */
24
    private $providers;
25
26
    /**
27
     * @param MetadataHierarchyProviderInterface[] $providers
28
     */
29
    public function __construct(array $providers = [])
30
    {
31
        $this->assertProvidersHaveCorrectType($providers);
32
33
        $this->providers = $providers;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getHierarchyByMetadataSubject(MetadataSubjectInterface $metadataSubject)
40
    {
41
        foreach ($this->providers as $provider) {
42
            if ($provider->supports($metadataSubject)) {
43
                return $provider->getHierarchyByMetadataSubject($metadataSubject);
44
            }
45
        }
46
47
        return [
48
            $metadataSubject->getMetadataIdentifier(),
49
            $metadataSubject->getMetadataClassIdentifier(),
50
        ];
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function supports(MetadataSubjectInterface $metadataSubject)
57
    {
58
        return true;
59
    }
60
61
    /**
62
     * @param MetadataHierarchyProviderInterface[] $providers
63
     */
64
    private function assertProvidersHaveCorrectType(array $providers)
65
    {
66
        foreach ($providers as $provider) {
67
            if ($provider instanceof MetadataHierarchyProviderInterface) {
68
                continue;
69
            }
70
71
            throw new \InvalidArgumentException(sprintf(
72
                'Metadata hierarchy provider should have type "%s"',
73
                MetadataHierarchyProviderInterface::class
74
            ));
75
        }
76
    }
77
}
78