PipelinesReferences::idNonPatternMatch()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 3
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File;
6
7
use InvalidArgumentException;
8
use Ktomk\Pipelines\Glob;
9
10
/**
11
 * Class PipelinesReferences
12
 *
13
 * Extract methods from Pipelines
14
 *
15
 * @package Ktomk\Pipelines\File
16
 */
17
abstract class PipelinesReferences extends Pipelines
18
{
19
    /**
20
     * @param Pipelines $pipelines
21
     * @param string $id
22
     *
23
     * @throws InvalidArgumentException
24
     * @throws ParseException
25
     *
26
     * @return null|Pipeline
27
     */
28 12
    protected static function byId(Pipelines $pipelines, $id)
29
    {
30 12
        if (!ReferenceTypes::isValidId($id)) {
31 1
            throw new InvalidArgumentException(sprintf("Invalid id '%s'", $id));
32
        }
33
34 11
        $file = $pipelines->file;
35 11
        if (!isset($pipelines->references[$id], $file)) {
36 1
            return null;
37
        }
38
39 10
        $ref = $pipelines->references[$id];
40 10
        if ($ref[2] instanceof Pipeline) {
41 3
            return $ref[2];
42
        }
43
44
        // bind to instance if yet an array
45 10
        if (!is_array($ref[2])) {
46 1
            throw new ParseException(sprintf('%s: named pipeline required', $id));
47
        }
48 9
        $pipeline = new Pipeline($file, $ref[2]);
49 9
        $ref[2] = $pipeline;
50
51 9
        return $pipeline;
52
    }
53
54
    /**
55
     * returns the id of the default pipeline in file or null if there is none
56
     *
57
     * @param Pipelines $pipelines
58
     *
59
     * @return null|string
60
     */
61 3
    protected static function idDefault(Pipelines $pipelines)
62
    {
63 3
        $id = ReferenceTypes::SEG_DEFAULT;
64
65 3
        if (!isset($pipelines->references[$id])) {
66 1
            return null;
67
        }
68
69 2
        return $id;
70
    }
71
72
    /**
73
     * get id of a pipeline
74
     *
75
     * @param Pipelines $pipelines
76
     * @param Pipeline $pipeline
77
     *
78
     * @return null|string
79
     */
80 2
    protected static function id(Pipelines $pipelines, Pipeline $pipeline)
81
    {
82 2
        foreach ($pipelines->references as $id => $reference) {
83 2
            if ($pipeline === $reference[2]) {
84 1
                return $id;
85
            }
86
        }
87
88 1
        return null;
89
    }
90
91
    /**
92
     * @param Pipelines $pipelines
93
     * @param null|string $type
94
     * @param null|string $reference
95
     *
96
     * @throws InvalidArgumentException
97
     *
98
     * @return null|string
99
     */
100 9
    protected static function idByTypeReference(Pipelines $pipelines, $type, $reference)
101
    {
102 9
        if (!ReferenceTypes::isPatternSection($type)) {
103 1
            throw new InvalidArgumentException(sprintf('Invalid type %s', var_export($type, true)));
104
        }
105
106 8
        list($resolve, $result) = self::idNonPatternMatch($pipelines, $type, $reference);
107 8
        if ($resolve) {
108 5
            return $result;
109
        }
110
111 6
        list($resolve, $result) = self::idPattern($pipelines, $type, (string)$reference);
112
113
        # fall-back to default pipeline on no match
114 6
        return $resolve ? $result : self::idDefault($pipelines);
115
    }
116
117
    /**
118
     * @param Pipelines $pipelines
119
     * @param null|string $section
120
     * @param null|string $reference
121
     *
122
     * @return array
123
     */
124 8
    private static function idNonPatternMatch(Pipelines $pipelines, $section, $reference)
125
    {
126
        # section is n/a, fall back to default pipeline
127 8
        if (!isset($pipelines->array[$section])) {
128 2
            return array(true, $pipelines->getIdDefault());
129
        }
130
131
        # check for direct (non-pattern) match
132 7
        if (isset($pipelines->array[$section][$reference])) {
133 3
            return array(true, "{$section}/{$reference}");
134
        }
135
136 6
        return array(false, null);
137
    }
138
139
    /**
140
     * get entry with largest pattern to match
141
     *
142
     * @param Pipelines $pipelines
143
     * @param string $section
144
     * @param string $reference
145
     *
146
     * @return array
147
     */
148 6
    private static function idPattern(Pipelines $pipelines, $section, $reference)
149
    {
150 6
        $patterns = array_keys($pipelines->array[$section]);
151
152 6
        $match = '';
153 6
        foreach ($patterns as $pattern) {
154 6
            $pattern = (string)$pattern;
155 6
            $result = Glob::match($pattern, $reference);
156 6
            if ($result && (strlen($pattern) > strlen($match))) {
157 4
                $match = $pattern;
158
            }
159
        }
160
161 6
        return array('' !== $match, "{$section}/{$match}");
162
    }
163
}
164