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

CollectionHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 8
dl 0
loc 28
ccs 12
cts 12
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 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\Version\VersionInterface;
24
25
/**
26
 * Class CollectionHandler
27
 * @author Gabriel Somoza <[email protected]>
28
 */
29
final class CollectionHandler extends AbstractFactoryHandler
30
{
31
    /**
32
     * Handle an "up" migration against a collection
33
     *
34
     * @param CollectionCommand $command
35
     *
36
     * @return \Baleen\Migrations\Shared\Collection\CollectionInterface
37
     */
38 1
    public function handle(CollectionCommand $command)
39
    {
40 1
        $target = $command->getTarget();
41 1
        $collection = $command->getCollection();
42 1
        $options = $command->getOptions();
43 1
        $direction = $options->getDirection();
44 1
        $comparator = $collection->getComparator()->withDirection($direction);
45
46
        // filter to only get versions that need to be migrated
47 1
        $filter = function (VersionInterface $v) use ($target, $comparator, $direction) {
48 1
            return ($direction->isUp() ^ $v->isMigrated()) // direction must be opposite to migration status
49 1
                    && $comparator->compare($v, $target) <= 0; // version must be before or be equal to target (not
50
                                                               // affected by direction because comparator is reversed)
51 1
        };
52 1
        $collection = $collection->filter($filter)->sort($comparator);
53
54 1
        return $this->createRunnerFor($collection)->run($target, $options);
55
    }
56
}
57