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

CollectionRunner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 10
dl 0
loc 70
ccs 30
cts 30
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
B run() 0 32 4
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\Runner\Event\Collection\CollectionAfterEvent;
24
use Baleen\Migrations\Service\Runner\Event\Collection\CollectionBeforeEvent;
25
use Baleen\Migrations\Shared\Collection\CollectionInterface;
26
use Baleen\Migrations\Shared\Event\Context\CollectionContext;
27
use Baleen\Migrations\Shared\Event\PublisherInterface;
28
use Baleen\Migrations\Version\Collection\Collection;
29
use Baleen\Migrations\Version\VersionInterface;
30
31
/**
32
 * Class CollectionRunner
33
 * @author Gabriel Somoza <[email protected]>
34
 */
35
final class CollectionRunner extends AbstractRunner
36
{
37
    /** @var CollectionInterface */
38
    private $collection;
39
40
    /** @var MigrationRunner */
41
    private $migrationRunner;
42
43
    /**
44
     * CollectionRunner constructor.
45
     * @param CollectionInterface $collection
46
     * @param RunnerInterface $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|RunnerInterface?

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