|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Subjects\FileResolver\FileResolverFactory |
|
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 Symfony\Component\DependencyInjection\ContainerInterface; |
|
24
|
|
|
use TechDivision\Import\Adapter\FilesystemAdapterFactoryInterface; |
|
25
|
|
|
use TechDivision\Import\Configuration\SubjectConfigurationInterface; |
|
26
|
|
|
use TechDivision\Import\Handlers\HandlerFactoryInterface; |
|
27
|
|
|
use TechDivision\Import\Loaders\FilteredLoader; |
|
28
|
|
|
use TechDivision\Import\Loaders\PregMatchFilteredLoader; |
|
29
|
|
|
use TechDivision\Import\Loaders\FilesystemLoader; |
|
30
|
|
|
use TechDivision\Import\Loaders\Filters\OkFileFilter; |
|
31
|
|
|
use TechDivision\Import\Adapter\FilesystemAdapterInterface; |
|
32
|
|
|
use TechDivision\Import\Utils\RegistryKeys; |
|
33
|
|
|
use TechDivision\Import\Services\RegistryProcessorInterface; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Factory for file resolver instances. |
|
37
|
|
|
* |
|
38
|
|
|
* @author Tim Wagner <[email protected]> |
|
39
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
40
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
41
|
|
|
* @link https://github.com/techdivision/import |
|
42
|
|
|
* @link http://www.techdivision.com |
|
43
|
|
|
*/ |
|
44
|
|
|
class FileResolverFactory implements FileResolverFactoryInterface |
|
45
|
|
|
{ |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The DI container instance. |
|
49
|
|
|
* |
|
50
|
|
|
* @var \Symfony\Component\DependencyInjection\ContainerInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $container; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The .OK file handler factory instance |
|
56
|
|
|
* |
|
57
|
|
|
* @var \TechDivision\Import\Handlers\HandlerFactoryInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
private $handlerFactory; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The registry processor instance. |
|
63
|
|
|
* |
|
64
|
|
|
* @var \TechDivision\Import\Services\RegistryProcessorInterface |
|
65
|
|
|
*/ |
|
66
|
|
|
private $registryProcessor; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Initialize the factory with the DI container instance. |
|
70
|
|
|
* |
|
71
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container The DI container instance |
|
72
|
|
|
* @param \TechDivision\Import\Handlers\HandlerFactoryInterface $handlerFactory The handler factory instance |
|
73
|
|
|
* @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __construct( |
|
76
|
|
|
ContainerInterface $container, |
|
77
|
|
|
HandlerFactoryInterface $handlerFactory, |
|
78
|
|
|
RegistryProcessorInterface $registryProcessor |
|
79
|
|
|
) { |
|
80
|
|
|
$this->container = $container; |
|
81
|
|
|
$this->handlerFactory = $handlerFactory; |
|
82
|
|
|
$this->registryProcessor = $registryProcessor; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Return's the container instance. |
|
87
|
|
|
* |
|
88
|
|
|
* @return \Symfony\Component\DependencyInjection\ContainerInterface The container instance |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function getContainer() : ContainerInterface |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->container; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Return's the registry processor instance. |
|
97
|
|
|
* |
|
98
|
|
|
* @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getRegistryProcessor() : RegistryProcessorInterface |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->registryProcessor; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Return's the .OK file handler factory instance. |
|
107
|
|
|
* |
|
108
|
|
|
* @return \TechDivision\Import\Handlers\HandlerFactoryInterface The .OK file handler factory instance |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function getHandlerFactory() : HandlerFactoryInterface |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->handlerFactory; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Return's the actual source directory. |
|
117
|
|
|
* |
|
118
|
|
|
* @param \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter The filesystem adapter to validate the source directory with |
|
119
|
|
|
* |
|
120
|
|
|
* @return string The actual source directory |
|
121
|
|
|
* @throws \Exception Is thrown, if the actual source directory can not be loaded |
|
122
|
|
|
*/ |
|
123
|
|
View Code Duplication |
protected function getSourceDir(FilesystemAdapterInterface $filesystemAdapter) : string |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
|
|
126
|
|
|
// try to load the actual status |
|
127
|
|
|
$status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
|
128
|
|
|
|
|
129
|
|
|
// query whether or not the configured source directory is available |
|
130
|
|
|
if (is_array($status) && $filesystemAdapter->isDir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) { |
|
131
|
|
|
return $sourceDir; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// throw an exception if the the actual source directory can not be loaded |
|
135
|
|
|
throw new \Exception(sprintf('Can\'t load source directory to create file writer instance for')); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Creates and returns the file resolver instance for the subject with the passed configuration. |
|
140
|
|
|
* |
|
141
|
|
|
* @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The subject to create the file resolver for |
|
142
|
|
|
* |
|
143
|
|
|
* @return \TechDivision\Import\Subjects\FileResolver\FileResolverInterface The file resolver instance |
|
144
|
|
|
*/ |
|
145
|
|
|
public function createFileResolver(SubjectConfigurationInterface $subject) : FileResolverInterface |
|
146
|
|
|
{ |
|
147
|
|
|
|
|
148
|
|
|
// load the DI container instance |
|
149
|
|
|
$container = $this->getContainer(); |
|
150
|
|
|
|
|
151
|
|
|
// load the proposed .OK file loader |
|
152
|
|
|
$handler = $this->getHandlerFactory()->createHandler($subject); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
// load the filesystem adapter instance |
|
155
|
|
|
$filesystemAdapter = $container->get($subject->getFilesystemAdapter()->getId()); |
|
156
|
|
|
|
|
157
|
|
|
// query whether or not we've a factory instance |
|
158
|
|
|
/** \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter */ |
|
159
|
|
|
if ($filesystemAdapter instanceof FilesystemAdapterFactoryInterface) { |
|
160
|
|
|
$filesystemAdapter = $filesystemAdapter->createFilesystemAdapter($subject); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// initialize the filter for the files that has to be imported |
|
164
|
|
|
// AND matches the .OK file, if one has been requested |
|
165
|
|
|
$filter = new OkFileFilter($handler, $subject, $this->getSourceDir($filesystemAdapter)); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
// initialize the loader for the files that has to be imported |
|
168
|
|
|
$filesystemLoader = new FilesystemLoader($filesystemAdapter); |
|
|
|
|
|
|
169
|
|
|
$filteredLoader = new FilteredLoader($filesystemLoader); |
|
170
|
|
|
$pregMatchFilteredloader = new PregMatchFilteredLoader($filteredLoader); |
|
171
|
|
|
$pregMatchFilteredloader->addFilter($filter); |
|
172
|
|
|
|
|
173
|
|
|
// create a new file resolver instance for the subject with the passed configuration |
|
174
|
|
|
/** @var \TechDivision\Import\Subjects\FileResolver\FileResolverInterface $fileResolver */ |
|
175
|
|
|
$fileResolver = $container->get($subject->getFileResolver()->getId()); |
|
176
|
|
|
$fileResolver->setFilesystemAdapter($filesystemAdapter); |
|
|
|
|
|
|
177
|
|
|
$fileResolver->setSubjectConfiguration($subject); |
|
178
|
|
|
$fileResolver->setLoader($pregMatchFilteredloader); |
|
179
|
|
|
|
|
180
|
|
|
// return the file resolver instance |
|
181
|
|
|
return $fileResolver; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
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.