|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Loaders\ProposedOkFilenameLoaderFactory |
|
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 2020 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\Loaders; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\RegistryKeys; |
|
24
|
|
|
use TechDivision\Import\Adapter\FilesystemAdapterInterface; |
|
25
|
|
|
use TechDivision\Import\Services\RegistryProcessorInterface; |
|
26
|
|
|
use TechDivision\Import\Loaders\Filters\DefaultOkFileFilter; |
|
27
|
|
|
use TechDivision\Import\Loaders\Sorters\DefaultOkFileSorter; |
|
28
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
29
|
|
|
use TechDivision\Import\Adapter\FilesystemAdapterFactoryInterface; |
|
30
|
|
|
use TechDivision\Import\Configuration\SubjectConfigurationInterface; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Factory implementation for a loader for the proposed .OK filenames. |
|
34
|
|
|
* |
|
35
|
|
|
* @author Tim Wagner <[email protected]> |
|
36
|
|
|
* @copyright 2020 TechDivision GmbH <[email protected]> |
|
37
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
38
|
|
|
* @link https://github.com/techdivision/import |
|
39
|
|
|
* @link http://www.techdivision.com |
|
40
|
|
|
*/ |
|
41
|
|
|
class ProposedOkFilenameLoaderFactory implements LoaderFactoryInterface |
|
42
|
|
|
{ |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The DI container instance. |
|
46
|
|
|
* |
|
47
|
|
|
* @var \Symfony\Component\DependencyInjection\ContainerInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
private $container; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The registry processor instance. |
|
53
|
|
|
* |
|
54
|
|
|
* @var \TechDivision\Import\Services\RegistryProcessorInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
private $registryProcessor; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Initialize the factory with the DI container instance. |
|
60
|
|
|
* |
|
61
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container The DI container instance |
|
62
|
|
|
* @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct( |
|
65
|
|
|
ContainerInterface $container, |
|
66
|
|
|
RegistryProcessorInterface $registryProcessor |
|
67
|
|
|
) { |
|
68
|
|
|
$this->container = $container; |
|
69
|
|
|
$this->registryProcessor = $registryProcessor; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Return's the container instance. |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Symfony\Component\DependencyInjection\ContainerInterface The container instance |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function getContainer() : ContainerInterface |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->container; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Return's the registry processor instance. |
|
84
|
|
|
* |
|
85
|
|
|
* @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function getRegistryProcessor() : RegistryProcessorInterface |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->registryProcessor; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Return's the actual source directory. |
|
94
|
|
|
* |
|
95
|
|
|
* @param \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter The filesystem adapter to validate the source directory with |
|
96
|
|
|
* |
|
97
|
|
|
* @return string The actual source directory |
|
98
|
|
|
* @throws \Exception Is thrown, if the actual source directory can not be loaded |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getSourceDir(FilesystemAdapterInterface $filesystemAdapter) : string |
|
101
|
|
|
{ |
|
102
|
|
|
|
|
103
|
|
|
// try to load the actual status |
|
104
|
|
|
$status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
|
105
|
|
|
|
|
106
|
|
|
// query whether or not the configured source directory is available |
|
107
|
|
|
if (is_array($status) && $filesystemAdapter->isDir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) { |
|
108
|
|
|
return $sourceDir; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// throw an exception if the the actual source directory can not be loaded |
|
112
|
|
|
throw new \Exception(sprintf('Can\'t load source directory to create file writer instance for')); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Create's and return's the apropriate loader instance. |
|
117
|
|
|
* |
|
118
|
|
|
* @return \TechDivision\Import\Loaders\LoaderInterface|null The loader instance |
|
119
|
|
|
*/ |
|
120
|
|
|
public function createLoader(SubjectConfigurationInterface $subject = null) : LoaderInterface |
|
121
|
|
|
{ |
|
122
|
|
|
|
|
123
|
|
|
// load the filesystem adapter instance |
|
124
|
|
|
$filesystemAdapter = $this->getContainer()->get($subject->getFilesystemAdapter()->getId()); |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
// query whether or not we've a factory instance |
|
127
|
|
|
if ($filesystemAdapter instanceof FilesystemAdapterFactoryInterface) { |
|
128
|
|
|
$filesystemAdapter = $filesystemAdapter->createFilesystemAdapter($subject); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// initialize the chain with the loader instances |
|
132
|
|
|
$filesystemLoader = new FilesystemLoader($filesystemAdapter); |
|
|
|
|
|
|
133
|
|
|
$filteredLoader = new FilteredLoader($filesystemLoader); |
|
134
|
|
|
$pregMatchFilteredLoader = new PregMatchFilteredLoader($filteredLoader); |
|
135
|
|
|
$proposedOkFileLoader = new ProposedOkFilenameLoader($pregMatchFilteredLoader); |
|
136
|
|
|
$proposedOkFileLoader->addFilter(new DefaultOkFileFilter()); |
|
137
|
|
|
$proposedOkFileLoader->addSorter(new DefaultOkFileSorter()); |
|
138
|
|
|
$proposedOkFileLoader->setSourceDir($this->getSourceDir($filesystemAdapter)); |
|
|
|
|
|
|
139
|
|
|
$proposedOkFileLoader->setFileResolverConfiguration($subject->getFileResolver()); |
|
140
|
|
|
|
|
141
|
|
|
// return the new loader instance |
|
142
|
|
|
return $proposedOkFileLoader; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: