|
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\Loaders\FilteredLoaderInterface; |
|
25
|
|
|
use TechDivision\Import\Adapter\FilesystemAdapterInterface; |
|
26
|
|
|
use TechDivision\Import\Services\RegistryProcessorInterface; |
|
27
|
|
|
use TechDivision\Import\Configuration\SubjectConfigurationInterface; |
|
28
|
|
|
use TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Abstract file resolver implementation. |
|
32
|
|
|
* |
|
33
|
|
|
* @author Tim Wagner <[email protected]> |
|
34
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
36
|
|
|
* @link https://github.com/techdivision/import |
|
37
|
|
|
* @link http://www.techdivision.com |
|
38
|
|
|
*/ |
|
39
|
|
View Code Duplication |
abstract class AbstractFileResolver implements FileResolverInterface |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The registry processor instance. |
|
44
|
|
|
* |
|
45
|
|
|
* @var \TechDivision\Import\Services\RegistryProcessorInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
private $registryProcessor; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The actual source directory to load the files from. |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private $sourceDir; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The subject configuraiton instance. |
|
58
|
|
|
* |
|
59
|
|
|
* @var \TechDivision\Import\Configuration\SubjectConfigurationInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
private $subjectConfiguration; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The filesystem adapter instance. |
|
65
|
|
|
* |
|
66
|
|
|
* @var \TechDivision\Import\Adapter\PhpFilesystemAdapterInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
private $filesystemAdapter; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The filesystem loader instance. |
|
72
|
|
|
* |
|
73
|
|
|
* @var \TechDivision\Import\Loaders\LoaderInterface |
|
74
|
|
|
*/ |
|
75
|
|
|
private $filesystemLoader; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Initializes the file resolver with the application and the registry instance. |
|
79
|
|
|
* |
|
80
|
|
|
* @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry instance |
|
81
|
3 |
|
* @param \TechDivision\Import\Loaders\FilteredLoaderInterface $filesystemLoader The filesystem loader instance |
|
82
|
|
|
*/ |
|
83
|
3 |
|
public function __construct(RegistryProcessorInterface $registryProcessor, FilteredLoaderInterface $filesystemLoader) |
|
84
|
3 |
|
{ |
|
85
|
3 |
|
$this->registryProcessor = $registryProcessor; |
|
86
|
|
|
$this->filesystemLoader = $filesystemLoader; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Return's the registry processor instance. |
|
91
|
|
|
* |
|
92
|
|
|
* @return \TechDivision\Import\Services\RegistryProcessorInterface The processor instance |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function getRegistryProcessor() : RegistryProcessorInterface |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->registryProcessor; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Return's the filesystem loader instance. |
|
101
|
|
|
* |
|
102
|
|
|
* @return \TechDivision\Import\Loaders\FilteredLoaderInterface The loader instance |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function getFilesystemLoader() : FilteredLoaderInterface |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->filesystemLoader; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Sets the actual source directory to load the files from. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $sourceDir The actual source directory |
|
113
|
|
|
* |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function setSourceDir(string $sourceDir) : void |
|
117
|
|
|
{ |
|
118
|
|
|
$this->sourceDir = $sourceDir; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Returns the actual source directory to load the files from. |
|
123
|
|
|
* |
|
124
|
|
|
* @return string The actual source directory |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function getSourceDir() : string |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->sourceDir; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns the file resolver configuration instance. |
|
133
|
|
|
* |
|
134
|
3 |
|
* @return \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface The configuration instance |
|
135
|
|
|
*/ |
|
136
|
3 |
|
protected function getFileResolverConfiguration() : FileResolverConfigurationInterface |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->getSubjectConfiguration()->getFileResolver(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Returns the suffix for the import files. |
|
143
|
|
|
* |
|
144
|
3 |
|
* @return string The suffix |
|
145
|
|
|
*/ |
|
146
|
3 |
|
protected function getSuffix() : string |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->getFileResolverConfiguration()->getSuffix(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Initializes the file resolver for the import process with the passed serial. |
|
153
|
|
|
* |
|
154
|
3 |
|
* @param string $serial The unique identifier of the actual import process |
|
155
|
|
|
* |
|
156
|
3 |
|
* @return void |
|
157
|
|
|
* @throws \Exception Is thrown if the configured source directory is not available |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function initialize(string $serial) : void |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
|
|
162
|
|
|
// load the actual status |
|
163
|
|
|
$status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
|
164
|
3 |
|
|
|
165
|
|
|
// query whether or not the configured source directory is available |
|
166
|
3 |
|
if ($this->getFilesystemAdapter()->isDir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) { |
|
167
|
|
|
// set the source directory, if it is accessible |
|
168
|
|
|
$this->setSourceDir($sourceDir); |
|
169
|
|
|
// return immediately |
|
170
|
|
|
return; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
// throw an exception otherwise |
|
174
|
1 |
|
throw new \Exception(sprintf('Configured source directory "%s" is not available!', $sourceDir)); |
|
175
|
|
|
} |
|
176
|
1 |
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Sets the subject configuration instance. |
|
179
|
|
|
* |
|
180
|
|
|
* @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subjectConfiguration The subject configuration |
|
181
|
|
|
* |
|
182
|
|
|
* @return void |
|
183
|
|
|
*/ |
|
184
|
|
|
public function setSubjectConfiguration(SubjectConfigurationInterface $subjectConfiguration) : void |
|
185
|
|
|
{ |
|
186
|
|
|
$this->subjectConfiguration = $subjectConfiguration; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Returns the subject configuration instance. |
|
191
|
|
|
* |
|
192
|
|
|
* @return \TechDivision\Import\Configuration\SubjectConfigurationInterface The subject configuration |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getSubjectConfiguration() : SubjectConfigurationInterface |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->subjectConfiguration; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Set's the filesystem adapter instance. |
|
201
|
|
|
* |
|
202
|
|
|
* @param \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter |
|
203
|
|
|
* |
|
204
|
|
|
* @return void |
|
205
|
|
|
*/ |
|
206
|
|
|
public function setFilesystemAdapter(FilesystemAdapterInterface $filesystemAdapter) : void |
|
207
|
|
|
{ |
|
208
|
|
|
$this->filesystemAdapter = $filesystemAdapter; |
|
|
|
|
|
|
209
|
3 |
|
} |
|
210
|
|
|
|
|
211
|
3 |
|
/** |
|
212
|
3 |
|
* Return's the filesystem adapter instance. |
|
213
|
|
|
* |
|
214
|
|
|
* @return \TechDivision\Import\Adapter\FilesystemAdapterInterface The filesystem adapter instance |
|
215
|
|
|
*/ |
|
216
|
|
|
public function getFilesystemAdapter() : FilesystemAdapterInterface |
|
217
|
|
|
{ |
|
218
|
|
|
return $this->filesystemAdapter; |
|
219
|
3 |
|
} |
|
220
|
|
|
|
|
221
|
3 |
|
/** |
|
222
|
|
|
* Loads the files from the source directory and return's them sorted. |
|
223
|
|
|
* |
|
224
|
|
|
* @param string $serial The unique identifier of the actual import process |
|
225
|
|
|
* |
|
226
|
|
|
* @return array The array with the files matching the subjects suffix |
|
227
|
|
|
* @throws \Exception Is thrown, when the source directory is NOT available |
|
228
|
|
|
*/ |
|
229
|
|
|
public function loadFiles(string $serial) : array |
|
230
|
|
|
{ |
|
231
|
|
|
|
|
232
|
|
|
// initialize the resolver |
|
233
|
|
|
// @TODO Check if the method can not be moved to object initialization |
|
234
|
|
|
$this->initialize($serial); |
|
235
|
|
|
|
|
236
|
|
|
// initialize the array with the files matching the suffix found in the source directory |
|
237
|
|
|
return $this->getFilesystemLoader()->load(sprintf('%s/*.%s', $this->getSourceDir(), $this->getSuffix())); |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
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.