ConvergeHandler::handle()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
ccs 32
cts 32
cp 1
rs 8.5806
cc 4
eloc 27
nc 6
nop 1
crap 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\DomainBus\Migrate\Converge;
21
22
use Baleen\Migrations\Migration\Options\Direction;
23
use Baleen\Migrations\Service\DomainBus\Migrate\AbstractRunnerHandler;
24
use Baleen\Migrations\Service\DomainBus\Migrate\Collection\CollectionCommand;
25
26
/**
27
 * Class ConvergeHandler
28
 *
29
 * @author Gabriel Somoza <[email protected]>
30
 */
31
final class ConvergeHandler
32
{
33
    /**
34
     * converge (v): come together from different directions so as eventually to meet.
35
     *
36
     * @param ConvergeCommand $command
37
     *
38
     * @return \Baleen\Migrations\Common\Collection\CollectionInterface
39
     */
40 8
    public function handle(ConvergeCommand $command)
41
    {
42 8
        $collection = $command->getCollection();
43 8
        $targetUp = $command->getTarget();
44 8
        $options = $command->getOptions();
45 8
        $domainBus = $command->getDomainBus();
46
47 8
        $position = $collection->getPosition($targetUp);
48 8
        $targetDown = $collection->getByPosition($position + 1);
49
50 8
        $changed = clone $collection;
51 8
        $changed->clear();
52
53 8
        $upCommand = new CollectionCommand(
54 8
            $collection,
55 8
            $targetUp,
56 8
            $options->withDirection(Direction::up()),
57 8
            $command->getVersionRepository()
58 8
        );
59 8
        $upChanges = $domainBus->handle($upCommand);
60 8
        if (!empty($upChanges)) {
61 8
            $changed->merge($upChanges);
62 8
        }
63
64
        // if we're not yet at the end of the queue (where no migrations can go down)
65 8
        if (null !== $targetDown) {
66 8
            $downCommand = new CollectionCommand(
67 8
                $collection,
68 8
                $targetDown,
69 8
                $options->withDirection(Direction::down()),
70 8
                $command->getVersionRepository()
71 8
            );
72 8
            $downChanges = $domainBus->handle($downCommand);
73 8
            if (!empty($downChanges)) {
74 8
                $changed->merge($downChanges);
75 8
            }
76 8
        }
77
78 8
        return $changed->sort();
79
    }
80
}
81