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

MigrationRunner::run()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
ccs 15
cts 15
cp 1
rs 8.8571
cc 3
eloc 14
nc 3
nop 2
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
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Baleen\Migrations\Service\Runner;
21
22
use Baleen\Migrations\Exception\Service\Runner\RunnerException;
23
use Baleen\Migrations\Migration\Command\MigrationBus;
24
use Baleen\Migrations\Migration\Command\MigrationBusInterface;
25
use Baleen\Migrations\Migration\OptionsInterface;
26
use Baleen\Migrations\Service\Runner\Event\Migration\MigrateAfterEvent;
27
use Baleen\Migrations\Service\Runner\Event\Migration\MigrateBeforeEvent;
28
use Baleen\Migrations\Shared\Event\Context\CollectionContextInterface;
29
use Baleen\Migrations\Shared\Event\PublisherInterface;
30
use Baleen\Migrations\Version\VersionInterface;
31
32
/**
33
 * Class MigrationRunner
34
 *
35
 * A Runner that emits domain events using an Emitter
36
 *
37
 * @author Gabriel Somoza <[email protected]>
38
 */
39
final class MigrationRunner extends AbstractRunner
40
{
41
    /** @var MigrationBusInterface */
42
    private $migrationBus;
43
44
    /**
45
     * MigrationRunner constructor.
46
     *
47
     * @param MigrationBusInterface $migrationBus
0 ignored issues
show
Documentation introduced by
Should the type for parameter $migrationBus not be null|MigrationBusInterface?

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...
48
     * @param PublisherInterface $publisher
0 ignored issues
show
Documentation introduced by
Should the type for parameter $publisher not be null|PublisherInterface?

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...
49
     * @param CollectionContextInterface $context
0 ignored issues
show
Documentation introduced by
Should the type for parameter $context not be null|CollectionContextInterface?

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...
50
     */
51 27
    public function __construct(
52
        MigrationBusInterface $migrationBus = null,
53
        PublisherInterface $publisher = null,
54
        CollectionContextInterface $context = null
55
    ) {
56 27
        if (null === $migrationBus) {
57 20
            $migrationBus = MigrationBus::createDefaultBus();
58 20
        }
59 27
        $this->migrationBus = $migrationBus;
60
61 27
        parent::__construct($publisher, $context);
62 27
    }
63
64
    /**
65
     * Runs a single version using the specified options
66
     *
67
     * @param VersionInterface $version
68
     * @param OptionsInterface $options
69
     *
70
     * @return VersionInterface|false
71
     *
72
     * @throws RunnerException
73
     */
74 23
    public function run(VersionInterface $version, OptionsInterface $options)
75
    {
76 23
        if (!$this->shouldMigrate($version, $options)) {
77 14
            if ($options->isExceptionOnSkip()) {
78 2
                throw new RunnerException(sprintf(
79 2
                    'Cowardly refusing to run %s() on a version that is already "%s" (ID: %s).',
80 2
                    $options->getDirection(),
81 2
                    $options->getDirection(),
82 2
                    $version->getId()
83 2
                ));
84
            }
85
86 12
            return false; // skip
87
        }
88
89
        // Dispatch MIGRATE_BEFORE
90 13
        $this->getPublisher()->publish(new MigrateBeforeEvent($version, $options, $this->getContext()));
0 ignored issues
show
Documentation introduced by
$this->getContext() is of type object<Baleen\Migrations...ntext\ContextInterface>, but the function expects a null|object<Baleen\Migra...ectionContextInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
92 13
        $this->doRun($version, $options);
93
94
        // this is safe because it won't get executed if an exception is thrown during migration
95 13
        $version->setMigrated($options->getDirection()->isUp());
96
97
        // Dispatch MIGRATE_AFTER
98 13
        $this->getPublisher()->publish(new MigrateAfterEvent($version, $options, $this->getContext()));
0 ignored issues
show
Documentation introduced by
$this->getContext() is of type object<Baleen\Migrations...ntext\ContextInterface>, but the function expects a null|object<Baleen\Migra...ectionContextInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
100 13
        return $version;
101
    }
102
103
    /**
104
     * Returns true if the operation is forced, or if the direction is the opposite to the state of the migration.
105
     *
106
     * @param VersionInterface $version
107
     * @param OptionsInterface $options
108
     *
109
     * @return bool
110
     */
111 23
    protected function shouldMigrate(VersionInterface $version, OptionsInterface $options)
112
    {
113 23
        return $options->isForced()
114 23
        || ($options->getDirection()->isUp() ^ $version->isMigrated()); // direction is opposite to state
115
    }
116
117
    /**
118
     * @param VersionInterface $version
119
     * @param OptionsInterface $options
120
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
121
     */
122 13
    protected function doRun(VersionInterface $version, OptionsInterface $options)
123
    {
124 13
        $command = $version->getMigrateCommand($options);
125 13
        $this->migrationBus->handle($command);
126 13
    }
127
}
128