Completed
Push — 15.x ( 74665b )
by Tim
02:57
created

SimpleFileResolver::countMatches()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Subjects\FileResolver\BunchFileResolver
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Subjects\FileResolver;
22
23
use TechDivision\Import\Utils\BunchKeys;
24
25
/**
26
 * Plugin that processes the subjects.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class SimpleFileResolver extends AbstractFileResolver
35
{
36
37
    /**
38
     * The regular expression used to load the files with.
39
     *
40
     * @var string
41
     */
42
    private $regex = '/^.*\/%s\\.%s$/';
43
44
    /**
45
     * The matches for the last processed CSV filename.
46
     *
47
     * @var array
48
     */
49
    private $matches = array();
50
51
    /**
52
     * Returns the regular expression used to load the files with.
53
     *
54
     * @return string The regular expression
55
     */
56
    protected function getRegex()
57
    {
58
        return $this->regex;
59
    }
60
61
    /**
62
     * Returns the number of matches found.
63
     *
64
     * @return integer The number of matches
65
     */
66
    protected function countMatches()
67
    {
68
        return sizeof($this->matches);
69
    }
70
71
    /**
72
     * Adds the passed match to the array with the matches.
73
     *
74
     * @param string $name  The name of the match
75
     * @param string $match The match itself
76
     *
77
     * @return void
78
     */
79
    protected function addMatch($name, $match)
80
    {
81
        $this->matches[strtolower($name)] = $match;
82
    }
83
84
    /**
85
     * Returns the match with the passed name.
86
     *
87
     * @param string $name The name of the match to return
88
     *
89
     * @return string|null The match itself
90
     */
91
    protected function getMatch($name)
92
    {
93
        if (isset($this->matches[$name])) {
94
            return $this->matches[$name];
95
        }
96
    }
97
98
    /**
99
     * Returns the elements the filenames consists of, converted to lowercase.
100
     *
101
     * @return array The array with the filename elements
102
     */
103
    protected function getPatternKeys()
104
    {
105
106
        // load the pattern keys from the configuration
107
        $patternKeys = $this->getPatternElements();
108
109
        // make sure that they are all lowercase
110
        array_walk($patternKeys, function (&$value) {
111
            $value = strtolower($value);
112
        });
113
114
        // return the pattern keys
115
        return $patternKeys;
116
    }
117
118
    /**
119
     * Returns the values to create the regex pattern from.
120
     *
121
     * @return array The array with the pattern values
122
     */
123
    protected function resolvePatternValues()
124
    {
125
126
        // initialize the array
127
        $elements = array();
128
129
        // load the pattern keys
130
        $patternKeys = $this->getPatternKeys();
131
132
        // prepare the pattern values
133
        foreach ($patternKeys as $element) {
134
            $elements[] = sprintf('(?<%s>%s)', $element, $this->resolvePatternValue($element));
135
        }
136
137
        // return the pattern values
138
        return $elements;
139
    }
140
141
    /**
142
     * Resolves the pattern value for the given element name.
143
     *
144
     * @param string $element The element name to resolve the pattern value for
145
     *
146
     * @return string|null The resolved pattern value
147
     */
148
    protected function resolvePatternValue($element)
149
    {
150
151
        // query whether or not matches has been found OR the counter element has been passed
152
        if ($this->countMatches() === 0 || BunchKeys::COUNTER === $element) {
153
            // prepare the method name for the callback to load the pattern value with
154
            $methodName = sprintf('get%s', ucfirst($element));
155
156
            // load the pattern value
157
            if (in_array($methodName, get_class_methods($this->getFileResolverConfiguration()))) {
158
                return call_user_func(array($this->getFileResolverConfiguration(), $methodName));
159
            }
160
161
            // stop processing
162
            return;
163
        }
164
165
        // try to load the pattern value from the matches
166
        return $this->getMatch($element);
167
    }
168
169
    /**
170
     * Prepares and returns the pattern for the regex to load the files from the
171
     * source directory for the passed subject.
172
     *
173
     * @return string The prepared regex pattern
174
     */
175
    protected function preparePattern()
176
    {
177
        return sprintf($this->getRegex(), implode($this->getElementSeparator(), $this->resolvePatternValues()), $this->getSuffix());
178
    }
179
180
    /**
181
     * Queries whether or not, the passed filename should be handled by the subject.
182
     *
183
     * @param string $filename The filename to query for
184
     *
185
     * @return boolean TRUE if the file should be handled, else FALSE
186
     */
187
    protected function shouldBeHandled($filename)
188
    {
189
190
        // initialize the array with the matches
191
        $matches = array();
192
193
        // update the matches, if the pattern matches
194
        if ($result = preg_match($this->preparePattern(), $filename, $matches)) {
195
            foreach ($matches as $name => $match) {
196
                $this->addMatch($name, $match);
197
            }
198
        }
199
200
        // stop processing, because the filename doesn't match the subjects pattern
201
        return (boolean) $result;
202
    }
203
204
    /**
205
     * Resets the file resolver to parse another source directory for new files.
206
     *
207
     * @return void
208
     */
209
    public function reset()
210
    {
211
        $this->matches = array();
212
    }
213
214
    /**
215
     * Returns the matches.
216
     *
217
     * @return array The array with the matches
218
     */
219
    public function getMatches()
220
    {
221
        return array_merge(array(BunchKeys::FILENAME => date('Ymd-His'), BunchKeys::COUNTER => 1), $this->matches);
222
    }
223
}
224