VersionRepository   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 96
ccs 38
cts 38
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchAll() 0 11 2
A update() 0 9 2
A save() 0 4 1
A delete() 0 4 1
A fetch() 0 7 3
A updateAll() 0 22 3
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\Delta\Repository;
21
22
use Baleen\Migrations\Common\Collection\CollectionInterface;
23
use Baleen\Migrations\Delta\Repository\Mapper\VersionMapperInterface;
24
use Baleen\Migrations\Delta\DeltaId;
25
use Baleen\Migrations\Delta\DeltaInterface;
26
27
/**
28
 * Class VersionRepository.
29
 *
30
 * @author Gabriel Somoza <[email protected]>
31
 */
32
final class VersionRepository implements VersionRepositoryInterface
33
{
34
    /** @var VersionMapperInterface */
35
    private $mapper;
36
37
    /**
38
     * VersionRepository constructor.
39
     *
40
     * @param VersionMapperInterface $mapper
41
     */
42 11
    public function __construct(VersionMapperInterface $mapper)
43
    {
44 11
        $this->mapper = $mapper;
45 11
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 4
    final public function fetchAll()
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
51
    {
52 4
        $ids = $this->mapper->fetchAll();
53
54
        return array_map(function ($id) {
55 3
            if (!is_object($id)) {
56 3
                $id = new DeltaId($id);
57 3
            }
58 3
            return $id;
59 4
        }, $ids);
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 4
    final public function update(DeltaInterface $version)
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
66
    {
67 4
        if ($version->isMigrated()) {
68 2
            $result = $this->save($version);
69 2
        } else {
70 2
            $result = $this->delete($version);
71
        }
72 4
        return $result;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 2
    public function updateAll(CollectionInterface $versions)
79
    {
80 2
        if ($versions->isEmpty()) {
81 1
            return true; // nothing to do - exit early
82
        }
83
84
        $mapToIds = function(DeltaInterface $v) {
85 1
            return $v->getId();
86 1
        };
87
        /** @var CollectionInterface $migrated */
88
        /** @var CollectionInterface $notMigrated */
89 1
        list($migrated, $notMigrated) = $versions->partition(function($i, DeltaInterface $v) {
90 1
            return $v->isMigrated();
91 1
        });
92 1
        $migratedIds = $migrated->map($mapToIds);
93 1
        $notMigratedIds = $notMigrated->map($mapToIds);
94
95 1
        $saveResult = $this->mapper->saveAll($migratedIds);
96 1
        $deleteResult = $this->mapper->deleteAll($notMigratedIds);
97
98 1
        return $saveResult && $deleteResult;
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104 2
    public function save(DeltaInterface $version)
105
    {
106 2
        return $this->mapper->save($version->getId());
107
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112 2
    public function delete(DeltaInterface $version)
113
    {
114 2
        return $this->mapper->delete($version->getId());
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120 1
    public function fetch($id)
121
    {
122 1
        if (!is_object($id) || !$id instanceof DeltaId) {
123 1
            $id = new DeltaId($id);
124 1
        }
125 1
        return $this->mapper->fetch($id);
126
    }
127
}
128