Completed
Pull Request — master (#15)
by Gabriel
05:24
created

Version::isSameIdentityAs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
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\Version;
21
22
use Baleen\Migrations\Service\MigrationBus\HasMigrationBusTrait;
23
use Baleen\Migrations\Service\MigrationBus\MigrateCommand;
24
use Baleen\Migrations\Service\MigrationBus\MigrationBusInterface;
25
use Baleen\Migrations\Migration\MigrationInterface;
26
use Baleen\Migrations\Migration\OptionsInterface;
27
use Baleen\Migrations\Shared\EntityInterface;
28
29
/**
30
 * {@inheritDoc}
31
 *
32
 * @author Gabriel Somoza <[email protected]>
33
 */
34
final class Version implements VersionInterface
35
{
36
    use HasMigrationBusTrait;
37
38
    /** @var VersionId */
39
    private $id;
40
41
    /** @var bool */
42
    private $migrated;
43
44
    /** @var MigrationInterface */
45
    private $migration;
46
47
    /**
48
     * @param MigrationInterface $migration
49
     * @param bool $migrated
50
     * @param null|VersionId $id Optionally force the ID to be something specific.
51
     * @param null|MigrationBusInterface $bus
52
     */
53 120
    public function __construct(
54
        MigrationInterface $migration,
55
        $migrated,
56
        VersionId $id = null,
57
        MigrationBusInterface $bus = null
58
    ) {
59 120
        if (null === $id) {
60 1
            $id = VersionId::fromMigration($migration);
61 1
        }
62 120
        $this->id = $id;
63
64 120
        $this->migration = $migration;
65 120
        $this->migrated = (bool) $migrated;
66 120
        $this->setMigrationBus($bus);
67 120
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 154
    public function getId()
73
    {
74 154
        return $this->id;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80 49
    public function __toString()
81
    {
82 49
        return $this->getId()->toString();
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 9
    public function isSameIdentityAs(EntityInterface $entity)
89
    {
90 9
        if (!$entity instanceof VersionInterface) {
91 1
            return false;
92
        }
93
94 9
        return $this->getId()->isSameValueAs($entity->getId());
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100 45
    public function isMigrated()
101
    {
102 45
        return $this->migrated;
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108 21
    public function getMigrationClassName()
109
    {
110 21
        return get_class($this->getMigration());
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 6
    public function getMigrationFileName()
117
    {
118 6
        $class = new \ReflectionClass($this->getMigration());
119 6
        $file = $class->getFileName();
120 6
        return $file;
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126 13
    public function migrate(OptionsInterface $options)
127
    {
128 13
        $command = new MigrateCommand($this->getMigration(), $options);
129 13
        $this->getMigrationBus()->handle($command);
130 13
        $this->migrated = $options->getDirection()->isUp();
131 13
    }
132
133
    /**
134
     * @return MigrationInterface
135
     */
136 44
    protected function getMigration() {
137 44
        return $this->migration;
138
    }
139
}
140