AggregateMigrationRepository::addRepositories()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 7
Ratio 53.85 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 13
ccs 10
cts 10
cp 1
rs 8.8571
cc 6
eloc 8
nc 3
nop 1
crap 6
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\Migration\Repository;
21
22
use Baleen\Migrations\Exception\InvalidArgumentException;
23
use Baleen\Migrations\Migration\Factory\FactoryInterface;
24
use Baleen\Migrations\Delta\Collection\Collection;
25
26
/**
27
 * A generic repository that can aggregate one or more other repositories
28
 *
29
 * @author Gabriel Somoza <[email protected]>
30
 */
31
final class AggregateMigrationRepository implements MigrationRepositoryInterface
32
{
33
    /** @var \SplStack */
34
    private $stack;
35
36
    /**
37
     * AggregateMigrationRepository constructor.
38
     */
39 13
    public function __construct()
40
    {
41 13
        $this->stack = new \SplStack();
42 13
    }
43
44
    /**
45
     * Adds a single repository to the stack
46
     *
47
     * @param MigrationRepositoryInterface $repo
48
     */
49 9
    public function addRepository(MigrationRepositoryInterface $repo)
50
    {
51 9
        $this->stack[] = $repo;
52 9
    }
53
54
    /**
55
     * Adds a set of repositories to the stack
56
     *
57
     * @param $repositories
58
     * @throws InvalidArgumentException
59
     */
60 9
    public function addRepositories($repositories)
61
    {
62 9 View Code Duplication
        if (!is_array($repositories) && (!is_object($repositories) || !$repositories instanceof \Traversable)) {
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...
63 1
            throw new InvalidArgumentException(sprintf(
64
                'Invalid argument provided for $repositories, expecting either an array or Traversable object, but' .
65 1
                ' "%s" given',
66 1
                is_object($repositories) ? get_class($repositories) : gettype($repositories)
67 1
            ));
68
        }
69 8
        foreach ($repositories as $repo) {
70 5
            $this->addRepository($repo);
71 8
        }
72 8
    }
73
74
    /**
75
     * Returns the stack
76
     *
77
     * @return \SplStack|MigrationRepositoryInterface[]
78
     */
79 11
    public function getRepositories()
80
    {
81 11
        return $this->stack;
82
    }
83
84
    /**
85
     * Resets the stack to the specified repositories
86
     *
87
     * @param array|\Traversable $repositories
88
     * @throws InvalidArgumentException
89
     */
90 6
    public function setRepositories($repositories)
91
    {
92 6 View Code Duplication
        if (!is_array($repositories) && (!is_object($repositories) || !$repositories instanceof \Traversable)) {
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...
93 1
            throw new InvalidArgumentException(sprintf(
94
                'Invalid argument provided for $repositories, expecting either an array or Traversable object, but' .
95 1
                ' "%s" given',
96 1
                is_object($repositories) ? get_class($repositories) : gettype($repositories)
97 1
            ));
98
        }
99 5
        $this->stack = new \SplStack();
100 5
        $this->addRepositories($repositories);
101 5
    }
102
103
    /**
104
     * Fetches all versions available to all repositories in the stack and returns them as a Linked collection.
105
     *
106
     * The returned collection contains versions groups sequentially into groups that correspond to each sub-repository.
107
     * Each of those groups is sorted with the repository's own comparator. Therefore, its strongly recommended not to
108
     * sort or modify the resulting collection.
109
     *
110
     * @return Collection
111
     */
112 2
    public function fetchAll()
113
    {
114 2
        $collection = new Collection();
115 2
        foreach ($this->getRepositories() as $repo) {
116
            /** @var MigrationRepositoryInterface $repo */
117 1
            $versions = $repo->fetchAll();
118 1
            $collection->merge($versions);
119 2
        }
120
121 2
        return $collection;
122
    }
123
124
    /**
125
     * Sets the migration factory for ALL repositories in the stack.
126
     *
127
     * @param FactoryInterface $factory
128
     */
129 1
    public function setMigrationFactory(FactoryInterface $factory)
130
    {
131 1
        foreach ($this->getRepositories() as $repo) {
132 1
            $repo->setMigrationFactory($factory);
133 1
        }
134 1
    }
135
}
136