LazyIdResolver::doResolve()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
ccs 15
cts 15
cp 1
rs 8.439
cc 5
eloc 13
nc 5
nop 2
crap 5
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\Delta\Collection\Resolver;
21
22
use Baleen\Migrations\Delta\Collection\Collection;
23
24
/**
25
 * Resolves version ID's. Should be lowest priority.
26
 *
27
 * @author Gabriel Somoza <[email protected]>
28
 */
29
final class LazyIdResolver extends AbstractResolver
30
{
31
    /**
32
     * @inheritdoc
33
     */
34 29
    public function doResolve($alias, Collection $collection)
35
    {
36
        // exit early if there's a full match
37 29
        if ($collection->contains($alias)) {
38 18
            return $collection->get($alias);
39
        }
40
41
        // lazy id search
42 12
        $length = strlen($alias);
43 12
        $candidate = null;
44 12
        foreach ($collection as $key => $version) {
45 12
            if (substr($key, 0, $length) === $alias) {
46 7
                if (null === $candidate) {
47
                    // make this version a candidate, but continue searching to see if any other items also meet the
48
                    // condition (in which case we'd know the $id being searched for is "ambiguous")
49 7
                    $candidate = $version;
50 7
                } else {
51
                    // the $id is ambiguous when used for lazy matching, therefore:
52 3
                    $candidate = null; // remove the candidate
53 3
                    break;
54
                }
55 7
            }
56 12
        }
57
58 12
        return $candidate;
59
    }
60
}
61