Completed
Pull Request — master (#15)
by Gabriel
03:36
created

VersionRepository::fetch()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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