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

ResolverStack::doResolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.4286
cc 3
eloc 6
nc 3
nop 2
crap 12
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