CollectionHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 10
dl 0
loc 47
ccs 19
cts 19
cp 1
rs 10

2 Methods

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