Completed
Pull Request — 11.x (#153)
by Tim
10:25 queued 10s
created

AbstractFileResolver::getSubjectConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Subjects\FileResolver\AbstractFileResolver
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\RegistryKeys;
24
use TechDivision\Import\ApplicationInterface;
25
use TechDivision\Import\Services\RegistryProcessorInterface;
26
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
27
28
/**
29
 * Abstract file resolver implementation.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import
35
 * @link      http://www.techdivision.com
36
 */
37
abstract class AbstractFileResolver implements FileResolverInterface
38
{
39
40
    /**
41
     * The registry processor instance.
42
     *
43
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
44
     */
45
    private $registryProcessor;
46
47
    /**
48
     * The appliation instance.
49
     *
50
     * @var \TechDivision\Import\ApplicationInterface
51
     */
52
    private $application;
53
54
    /**
55
     * The actual source directory to load the files from.
56
     *
57
     * @var string
58
     */
59
    private $sourceDir;
60
61
    /**
62
     * The OK file suffix to use.
63
     *
64
     * @var string
65
     */
66
    private $okFileSuffix = 'ok';
0 ignored issues
show
Unused Code introduced by
The property $okFileSuffix is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
67
68
    /**
69
     * The subject configuraiton instance.
70
     *
71
     * @var \TechDivision\Import\Configuration\SubjectConfigurationInterface
72
     */
73
    private $subjectConfiguration;
74
75
    /**
76
     * Initializes the file resolver with the application and the registry instance.
77
     *
78
     * @param \TechDivision\Import\ApplicationInterface                $application       The application instance
79
     * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry instance
80
     */
81 3
    public function __construct(ApplicationInterface $application, RegistryProcessorInterface $registryProcessor)
82
    {
83 3
        $this->application = $application;
84 3
        $this->registryProcessor = $registryProcessor;
85 3
    }
86
87
    /**
88
     * Returns the registry processor instance.
89
     *
90
     * @return \TechDivision\Import\Services\RegistryProcessorInterface The processor instance
91
     */
92
    protected function getRegistryProcessor()
93
    {
94
        return $this->registryProcessor;
95
    }
96
97
    /**
98
     * Return's the application instance.
99
     *
100
     * @return \TechDivision\Import\ApplicationInterface The application instance
101
     */
102
    protected function getApplication()
103
    {
104
        return $this->application;
105
    }
106
107
    /**
108
     * Sets the actual source directory to load the files from.
109
     *
110
     * @param string $sourceDir The actual source directory
111
     *
112
     * @return void
113
     */
114
    protected function setSourceDir($sourceDir)
115
    {
116
        $this->sourceDir = $sourceDir;
117
    }
118
119
    /**
120
     * Returns the actual source directory to load the files from.
121
     *
122
     * @return string The actual source directory
123
     */
124
    protected function getSourceDir()
125
    {
126
        return $this->sourceDir;
127
    }
128
129
    /**
130
     * Returns the file resolver configuration instance.
131
     *
132
     * @return \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface The configuration instance
133
     */
134 3
    protected function getFileResolverConfiguration()
135
    {
136 3
        return $this->getSubjectConfiguration()->getFileResolver();
137
    }
138
139
    /**
140
     * Returns the delement separator char.
141
     *
142
     *  @return string The element separator char
143
     */
144 3
    protected function getElementSeparator()
145
    {
146 3
        return $this->getFileResolverConfiguration()->getElementSeparator();
147
    }
148
149
    /**
150
     * Returns the elements the filenames consists of.
151
     *
152
     * @return array The array with the filename elements
153
     */
154 3
    protected function getPatternElements()
155
    {
156 3
        return $this->getFileResolverConfiguration()->getPatternElements();
157
    }
158
159
    /**
160
     * Returns the suffix for the import files.
161
     *
162
     * @return string The suffix
163
     */
164 3
    protected function getSuffix()
165
    {
166 3
        return $this->getFileResolverConfiguration()->getSuffix();
167
    }
168
169
    /**
170
     * Returns the OK file suffix to use.
171
     *
172
     * @return string The OK file suffix
173
     */
174 1
    protected function getOkFileSuffix()
175
    {
176 1
        return $this->getFileResolverConfiguration()->getOkFileSuffix();
177
    }
178
179
    /**
180
     * Initializes the file resolver for the import process with the passed serial.
181
     *
182
     * @param string $serial The unique identifier of the actual import process
183
     *
184
     * @return void
185
     * @throws \Exception Is thrown if the configured source directory is not available
186
     */
187 View Code Duplication
    protected function initialize($serial)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Unused Code introduced by
The parameter $serial is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
188
    {
189
190
        // load the actual status
191
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
192
193
        // query whether or not the configured source directory is available
194
        if (!is_dir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) {
195
            throw new \Exception(sprintf('Configured source directory "%s" is not available!', $sourceDir));
196
        }
197
198
        // set the source directory
199
        $this->setSourceDir($sourceDir);
200
    }
201
202
    /**
203
     * Sets the subject configuration instance.
204
     *
205
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subjectConfiguration The subject configuration
206
     *
207
     * @return void
208
     */
209 3
    public function setSubjectConfiguration(SubjectConfigurationInterface $subjectConfiguration)
210
    {
211 3
        $this->subjectConfiguration = $subjectConfiguration;
212 3
    }
213
214
    /**
215
     * Returns the subject configuration instance.
216
     *
217
     * @return \TechDivision\Import\Configuration\SubjectConfigurationInterface The subject configuration
218
     */
219 3
    public function getSubjectConfiguration()
220
    {
221 3
        return $this->subjectConfiguration;
222
    }
223
224
    /**
225
     * Loads the files from the source directory and return's them sorted.
226
     *
227
     * @param string $serial The unique identifier of the actual import process
228
     *
229
     * @return array The array with the files matching the subjects suffix
230
     * @throws \Exception Is thrown, when the source directory is NOT available
231
     */
232
    public function loadFiles($serial)
233
    {
234
235
        // clear the filecache
236
        clearstatcache();
237
238
        // initialize the resolver
239
        $this->initialize($serial);
240
241
        // initialize the array with the files matching the suffix found in the source directory
242
        $files = glob(sprintf('%s/*.%s', $this->getSourceDir(), $this->getSuffix()));
243
244
        // sort the files for the apropriate order
245
        usort($files, function ($a, $b) {
246
            return strcmp($a, $b);
247
        });
248
249
        // return the sorted files
250
        return $files;
251
    }
252
}
253