Completed
Pull Request — master (#15)
by Gabriel
05:24
created

ResolverStack   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 3
Metric Value
wmc 8
c 5
b 1
f 3
lcom 1
cbo 3
dl 0
loc 48
ccs 6
cts 6
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 14 5
A doResolve() 0 10 3
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Baleen\Migrations\Version\Collection\Resolver;
21
22
use Baleen\Migrations\Exception\InvalidArgumentException;
23
use Baleen\Migrations\Version\Collection\Collection;
24
25
/**
26
 * Class ResolverStack
27
 *
28
 * @author Gabriel Somoza <[email protected]>
29
 */
30
final class ResolverStack extends AbstractResolver
31
{
32
    /** @var ResolverInterface[] */
33
    protected $resolvers = [];
34
35
    /**
36
     * ResolverStack constructor.
37
     *
38
     * @param ResolverInterface[] $resolvers
39
     *
40
     * @param bool $cacheEnabled
41
     * @throws InvalidArgumentException
42
     */
43 123
    public function __construct(array $resolvers, $cacheEnabled = true)
44
    {
45 123
        foreach ($resolvers as $resolver) {
46 123
            if (!is_object($resolver) || !$resolver instanceof ResolverInterface) {
47 1
                throw new InvalidArgumentException(sprintf(
48 1
                    'Invalid resolver of type "%s". Expected instance of "%s".',
49 1
                    is_object($resolver) ? get_class($resolver) : gettype($resolver),
50
                    ResolverInterface::class
51
                ));
52
            }
53
        }
54
        $this->resolvers = $resolvers;
55
        parent::__construct($cacheEnabled);
56
    }
57
58
    /**
59
     * Resolves an alias
60
     *
61
     * @param string $alias
62
     * @param Collection $collection
63
     *
64
     * @return \Baleen\Migrations\Version\VersionInterface|null
65
     * @throws \Baleen\Migrations\Exception\Version\Collection\ResolverException
66
     */
67
    public function doResolve($alias, Collection $collection)
68
    {
69
        foreach ($this->resolvers as $resolver) {
70
            $result = $resolver->resolve($alias, $collection);
71
            if ($result !== null) {
72
                return $result;
73
            }
74
        }
75
        return null;
76
    }
77
}
78