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

VersionRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 81
ccs 29
cts 29
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 saveCollection() 0 7 1
A save() 0 4 1
A delete() 0 4 1
A fetch() 0 7 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\Version\Collection\Collection;
23
use Baleen\Migrations\Version\Repository\Mapper\VersionMapperInterface;
24
use Baleen\Migrations\Version\VersionId;
25
use Baleen\Migrations\Version\VersionInterface;
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 10
    public function __construct(VersionMapperInterface $mapper)
43
    {
44 10
        $this->mapper = $mapper;
45 10
    }
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 VersionId($id);
57 3
            }
58 3
            return $id;
59 4
        }, $ids);
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 4
    final public function update(VersionInterface $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
    public function saveCollection(Collection $versions)
79
    {
80 1
        $ids = $versions->map(function (VersionInterface $v) {
81 1
            return $v->getId();
82 1
        });
83 1
        return $this->mapper->saveAll($ids);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 2
    public function save(VersionInterface $version)
90
    {
91 2
        return $this->mapper->save($version->getId());
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97 2
    public function delete(VersionInterface $version)
98
    {
99 2
        return $this->mapper->delete($version->getId());
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105 1
    public function fetch($id)
106
    {
107 1
        if (!is_object($id) || !$id instanceof VersionId) {
108 1
            $id = new VersionId($id);
109 1
        }
110 1
        return $this->mapper->fetch($id);
111
    }
112
}
113