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

CollectionHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
c 3
b 0
f 0
lcom 0
cbo 9
dl 0
loc 45
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 23 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\Command\Migrate\Collection;
21
22
use Baleen\Migrations\Service\Command\Migrate\AbstractFactoryHandler;
23
use Baleen\Migrations\Service\Runner\Factory\CollectionRunnerFactoryInterface;
24
use Baleen\Migrations\Service\Runner\Factory\CreatesCollectionRunnerTrait;
25
use Baleen\Migrations\Version\Collection\Collection;
26
use Baleen\Migrations\Version\VersionInterface;
27
28
/**
29
 * Class CollectionHandler
30
 *
31
 * @author Gabriel Somoza <[email protected]>
32
 */
33
final class CollectionHandler
34
{
35
    use CreatesCollectionRunnerTrait;
36
37
    /**
38
     * CollectionHandler constructor.
39
     * @param CollectionRunnerFactoryInterface $factory
40
     */
41 3
    public function __construct(CollectionRunnerFactoryInterface $factory)
42
    {
43 3
        $this->setFactory($factory);
44 3
    }
45
46
47
    /**
48
     * Handle an "up" migration against a collection
49
     *
50
     * @param CollectionCommand $command
51
     *
52
     * @return bool The result of saving the collection of updated versions to the repository
53
     */
54 1
    public function handle(CollectionCommand $command)
55
    {
56 1
        $target = $command->getTarget();
57 1
        $collection = $command->getCollection();
58 1
        $options = $command->getOptions();
59 1
        $direction = $options->getDirection();
60 1
        $comparator = $collection->getComparator()->withDirection($direction);
61
62
        // filter to only get versions that need to be migrated
63 1
        $filter = function (VersionInterface $v) use ($target, $comparator, $direction) {
64 1
            return ($direction->isUp() ^ $v->isMigrated()) // direction must be opposite to migration status
65 1
                    && $comparator->compare($v, $target) <= 0; // version must be before or be equal to target (not
66
                                                               // affected by direction because comparator is reversed)
67 1
        };
68 1
        $scheduled = $collection->filter($filter)->sort($comparator);
69
        // we don't check if $scheduled is empty after filtering because the collection-before and -after events should
70
        // still be triggered by the runner
71
72
        /** @var Collection $changed */
73 1
        $changed = $this->createRunnerFor($scheduled)->run($target, $options);
74
75 1
        return $command->getVersionRepository()->updateAll($changed);
76
    }
77
}
78