Completed
Branch ddd-changes (96d3bc)
by Gabriel
05:08
created

MigrationRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 56
ccs 22
cts 22
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A fetchAll() 0 19 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\Migration\Repository\Mapper\DefinitionInterface;
23
use Baleen\Migrations\Migration\Repository\Mapper\RepositoryMapperInterface;
24
use Baleen\Migrations\Version\Collection\Collection;
25
use Baleen\Migrations\Version\Comparator\ComparatorInterface;
26
use Baleen\Migrations\Version\Comparator\MigrationComparator;
27
use Baleen\Migrations\Version\Repository\VersionRepositoryInterface as VersionRepositoryInterface;
28
use Baleen\Migrations\Version\Version;
29
use Baleen\Migrations\Version\VersionId;
30
31
/**
32
 * Class MigrationRepository.
33
 *
34
 * @author Gabriel Somoza <[email protected]>
35
 */
36
final class MigrationRepository implements MigrationRepositoryInterface
37
{
38
    /** @var ComparatorInterface */
39
    private $comparator = null;
40
41
    /** @var VersionRepositoryInterface */
42
    private $storage;
43
44
    /** @var RepositoryMapperInterface */
45
    private $mapper;
46
47
    /**
48
     * MigrationRepository constructor
49
     *
50
     * @param VersionRepositoryInterface $storage
51
     * @param RepositoryMapperInterface $mapper
52
     * @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...
53
     */
54 2
    public function __construct(
55
        VersionRepositoryInterface $storage,
56
        RepositoryMapperInterface $mapper,
57
        ComparatorInterface $comparator = null
58
    ) {
59 2
        if (null === $comparator) {
60
            // this is the default because we're hashing IDs by default
61 2
            $comparator = new MigrationComparator();
62 2
        }
63 2
        $this->comparator = $comparator;
64
65 2
        $this->storage = $storage;
66 2
        $this->mapper = $mapper;
67 2
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 2
    public function fetchAll()
73
    {
74 2
        $definitions = $this->mapper->fetchAll();
75 2
        $stored = array_map(function (VersionId $id) {
76 2
            return $id->toString();
77 2
        }, $this->storage->fetchAll());
78
79 2
        $collection = new Collection();
80 2
        foreach ($definitions as $definition) {
81
            /** @var DefinitionInterface $definition */
82 2
            $migration = $definition->getMigration();
83 2
            $id = $definition->getId();
84 2
            $migrated = in_array($id->toString(), $stored);
85 2
            $version = new Version($migration, $migrated, $id);
86 2
            $collection->add($version);
87 2
        }
88
89 2
        return $collection->sort($this->comparator);
90
    }
91
}
92