MigrationRepository::fetchAll()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
ccs 14
cts 14
cp 1
rs 9.4285
cc 2
eloc 13
nc 2
nop 0
crap 2
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
 * <https://github.com/baleen/migrations>.
18
 */
19
20
namespace Baleen\Migrations\Migration\Repository;
21
22
use Baleen\Migrations\Service\MigrationBus\HasMigrationBusTrait;
23
use Baleen\Migrations\Service\MigrationBus\MigrationBusInterface;
24
use Baleen\Migrations\Migration\Repository\Mapper\DefinitionInterface;
25
use Baleen\Migrations\Migration\Repository\Mapper\MigrationMapperInterface;
26
use Baleen\Migrations\Delta\Collection\Collection;
27
use Baleen\Migrations\Delta\Comparator\ComparatorInterface;
28
use Baleen\Migrations\Delta\Comparator\MigrationComparator;
29
use Baleen\Migrations\Delta\Repository\VersionRepositoryInterface as VersionRepositoryInterface;
30
use Baleen\Migrations\Delta\Delta;
31
use Baleen\Migrations\Delta\DeltaId;
32
33
/**
34
 * Class MigrationRepository.
35
 *
36
 * @author Gabriel Somoza <[email protected]>
37
 */
38
final class MigrationRepository implements MigrationRepositoryInterface
39
{
40
    use HasMigrationBusTrait;
41
42
    /** @var ComparatorInterface */
43
    private $comparator = null;
44
45
    /** @var VersionRepositoryInterface */
46
    private $storage;
47
48
    /** @var MigrationMapperInterface */
49
    private $mapper;
50
51
    /**
52
     * MigrationRepository constructor
53
     *
54
     * @param VersionRepositoryInterface $storage
55
     * @param MigrationMapperInterface $mapper
56
     * @param ComparatorInterface $comparator
0 ignored issues
show
Documentation introduced by
Should the type for parameter $comparator not be null|ComparatorInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
57
     * @param MigrationBusInterface $migrationBus
0 ignored issues
show
Documentation introduced by
Should the type for parameter $migrationBus not be null|MigrationBusInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
58
     */
59 2
    public function __construct(
60
        VersionRepositoryInterface $storage,
61
        MigrationMapperInterface $mapper,
62
        ComparatorInterface $comparator = null,
63
        MigrationBusInterface $migrationBus = null
64
    ) {
65 2
        if (null === $comparator) {
66
            // this is the default because we're hashing IDs by default
67 2
            $comparator = new MigrationComparator();
68 2
        }
69 2
        $this->comparator = $comparator;
70
71 2
        $this->setMigrationBus($migrationBus);
72
73 2
        $this->storage = $storage;
74 2
        $this->mapper = $mapper;
75 2
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80 2
    public function fetchAll()
81
    {
82 2
        $definitions = $this->mapper->fetchAll();
83 2
        $stored = array_map(function (DeltaId $id) {
84 2
            return $id->toString();
85 2
        }, $this->storage->fetchAll());
86
87 2
        $collection = new Collection();
88 2
        foreach ($definitions as $definition) {
89
            /** @var DefinitionInterface $definition */
90 2
            $migration = $definition->getMigration();
91 2
            $id = $definition->getId();
92 2
            $migrated = in_array($id->toString(), $stored);
93 2
            $version = new Delta($migration, $migrated, $id, $this->getMigrationBus());
94 2
            $collection->add($version);
95 2
        }
96
97 2
        return $collection->sort($this->comparator);
98
    }
99
}
100