CollectionRunner::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 3
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
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Baleen\Migrations\Service\Runner;
21
22
use Baleen\Migrations\Migration\OptionsInterface;
23
use Baleen\Migrations\Service\DomainBus\HasCollectionTrait;
24
use Baleen\Migrations\Service\Runner\Event\Collection\CollectionAfterEvent;
25
use Baleen\Migrations\Service\Runner\Event\Collection\CollectionBeforeEvent;
26
use Baleen\Migrations\Common\Collection\CollectionInterface;
27
use Baleen\Migrations\Common\Event\Context\CollectionContext;
28
use Baleen\Migrations\Common\Event\MutePublisher;
29
use Baleen\Migrations\Common\Event\Publisher\HasInternalPublisherTrait;
30
use Baleen\Migrations\Common\Event\PublisherInterface;
31
use Baleen\Migrations\Delta\Collection\Collection;
32
use Baleen\Migrations\Delta\DeltaInterface;
33
34
/**
35
 * Class CollectionRunner
36
 * @author Gabriel Somoza <[email protected]>
37
 */
38
final class CollectionRunner implements RunnerInterface
39
{
40
    use HasCollectionTrait;
41
    use HasInternalPublisherTrait;
42
43
    /** @var MigrationRunnerInterface */
44
    private $migrationRunner;
45
46
    /**
47
     * CollectionRunner constructor.
48
     * @param CollectionInterface $collection
49
     * @param MigrationRunnerInterface $migrationRunner Will be use to run each individual migration
0 ignored issues
show
Documentation introduced by
Should the type for parameter $migrationRunner not be null|MigrationRunnerInterface?

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
     * @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...
51
     */
52 19
    public function __construct(
53
        CollectionInterface $collection,
54
        MigrationRunnerInterface $migrationRunner = null,
55
        PublisherInterface $publisher = null
56
    ) {
57 19
        if (null === $migrationRunner) {
58 19
            $migrationRunner = new MigrationRunner($publisher);
59 19
        }
60 19
        $this->migrationRunner = $migrationRunner;
61
62 19
        $this->setCollection($collection);
63 19
        $this->setPublisher($publisher);
64 19
    }
65
66
    /**
67
     * Runs a collection of versions towards the specified goal and using the specified options
68
     *
69
     * @param DeltaInterface $target
70
     * @param OptionsInterface $options
71
     *
72
     * @return CollectionAfterEvent
73
     */
74 18
    public function run(DeltaInterface $target, OptionsInterface $options)
75
    {
76 18
        $current = 1;
77 18
        $collection = $this->getCollection();
78 18
        $context = CollectionContext::createWithProgress(max($collection->count(), 1), $current);
79 18
        $migrationRunner = $this->migrationRunner->withContext($context);
80
81 18
        $this->getPublisher()->publish(new CollectionBeforeEvent($target, $options, $collection));
82
83 18
        $modified = new Collection();
84 18
        $comparator = $collection->getComparator();
85
86
        // IMPROVE: add tests to see if rewind is necessary
87 18
        $collection->first(); // rewind
88 18
        foreach ($collection as $version) {
89 18
            $context->getProgress()->update($current);
90 18
            $result = $migrationRunner->run($version, $options);
91 18
            if ($result) {
92 11
                $modified->add($version);
93 11
            }
94 18
            if ($comparator->compare($version, $target) >= 0) {
95 18
                break;
96
            }
97 12
            $current += 1;
98 18
        }
99
100 18
        $event = new CollectionAfterEvent($target, $options, $modified);
101 18
        $this->getPublisher()->publish($event);
102
103 18
        return $event;
104
    }
105
}
106