ResolverStack::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 7
Ratio 50 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 14
ccs 11
cts 11
cp 1
rs 8.8571
cc 5
eloc 9
nc 3
nop 2
crap 5
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\Delta\Collection\Resolver;
21
22
use Baleen\Migrations\Exception\InvalidArgumentException;
23
use Baleen\Migrations\Delta\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 View Code Duplication
            if (!is_object($resolver) || !$resolver instanceof ResolverInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 1
                ));
52
            }
53 122
        }
54 122
        $this->resolvers = $resolvers;
55 122
        parent::__construct($cacheEnabled);
56 122
    }
57
58
    /**
59
     * Resolves an alias
60
     *
61
     * @param string $alias
62
     * @param Collection $collection
63
     *
64
     * @return \Baleen\Migrations\Delta\DeltaInterface|null
65
     * @throws \Baleen\Migrations\Exception\Version\Collection\ResolverException
66
     */
67 32
    public function doResolve($alias, Collection $collection)
68
    {
69 32
        foreach ($this->resolvers as $resolver) {
70 32
            $result = $resolver->resolve($alias, $collection);
71 32
            if ($result !== null) {
72 32
                return $result;
73
            }
74 32
        }
75 1
        return null;
76
    }
77
}
78