Completed
Pull Request — master (#15)
by Gabriel
03:16
created

CollectionRunner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 6
c 3
b 0
f 1
lcom 1
cbo 12
dl 0
loc 68
ccs 31
cts 31
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
B run() 0 31 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\DomainBus\HasCollectionTrait;
24
use Baleen\Migrations\Service\Runner\Event\Collection\CollectionAfterEvent;
25
use Baleen\Migrations\Service\Runner\Event\Collection\CollectionBeforeEvent;
26
use Baleen\Migrations\Shared\Collection\CollectionInterface;
27
use Baleen\Migrations\Shared\Event\Context\CollectionContext;
28
use Baleen\Migrations\Shared\Event\Publisher\HasInternalPublisherTrait;
29
use Baleen\Migrations\Shared\Event\PublisherInterface;
30
use Baleen\Migrations\Version\Collection\Collection;
31
use Baleen\Migrations\Version\VersionInterface;
32
33
/**
34
 * Class CollectionRunner
35
 * @author Gabriel Somoza <[email protected]>
36
 */
37
final class CollectionRunner implements RunnerInterface
38
{
39
    use HasCollectionTrait;
40
    use HasInternalPublisherTrait;
41
42
    /** @var MigrationRunnerInterface */
43
    private $migrationRunner;
44
45
    /**
46
     * CollectionRunner constructor.
47
     * @param CollectionInterface $collection
48
     * @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...
49
     * @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...
50
     */
51 19
    public function __construct(
52
        CollectionInterface $collection,
53
        MigrationRunnerInterface $migrationRunner = null,
54
        PublisherInterface $publisher = null
55
    ) {
56 19
        if (null === $migrationRunner) {
57 19
            $migrationRunner = new MigrationRunner($publisher);
58 19
        }
59 19
        $this->migrationRunner = $migrationRunner;
60
61 19
        $this->setCollection($collection);
62 19
        $this->setPublisher($publisher);
63 19
    }
64
65
    /**
66
     * Runs a collection of versions towards the specified goal and using the specified options
67
     *
68
     * @param VersionInterface $target
69
     * @param OptionsInterface $options
70
     *
71
     * @return CollectionAfterEvent
72
     */
73 18
    public function run(VersionInterface $target, OptionsInterface $options)
74
    {
75 18
        $current = 1;
76 18
        $collection = $this->getCollection();
77 18
        $context = CollectionContext::createWithProgress(max($collection->count(), 1), $current);
78 18
        $migrationRunner = $this->migrationRunner->withContext($context);
79
80 18
        $this->getPublisher()->publish(new CollectionBeforeEvent($target, $options, $collection));
81
82 18
        $modified = new Collection();
83 18
        $comparator = $collection->getComparator();
84
85
        // IMPROVE: add tests to see if rewind is necessary
86 18
        $collection->first(); // rewind
87 18
        foreach ($collection as $version) {
88 18
            $context->getProgress()->update($current);
89 18
            $result = $migrationRunner->run($version, $options);
90 18
            if ($result) {
91 11
                $modified->add($version);
92 11
            }
93 18
            if ($comparator->compare($version, $target) >= 0) {
94 18
                break;
95 1
            }
96 12
            $current += 1;
97 18
        }
98
99 18
        $event = new CollectionAfterEvent($target, $options, $modified);
100 18
        $this->getPublisher()->publish($event);
101
102 18
        return $event;
103
    }
104
}
105