Code Duplication    Length = 194-201 lines in 2 locations

src/Subjects/FileResolver/AbstractFileResolver.php 1 location

@@ 39-239 (lines=201) @@
36
 * @link      https://github.com/techdivision/import
37
 * @link      http://www.techdivision.com
38
 */
39
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
     * @param \TechDivision\Import\Loaders\FilteredLoaderInterface     $filesystemLoader  The filesystem loader instance
82
     */
83
    public function __construct(RegistryProcessorInterface $registryProcessor, FilteredLoaderInterface $filesystemLoader)
84
    {
85
        $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
     * @return \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface The configuration instance
135
     */
136
    protected function getFileResolverConfiguration() : FileResolverConfigurationInterface
137
    {
138
        return $this->getSubjectConfiguration()->getFileResolver();
139
    }
140
141
    /**
142
     * Returns the suffix for the import files.
143
     *
144
     * @return string The suffix
145
     */
146
    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
     * @param string $serial The unique identifier of the actual import process
155
     *
156
     * @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
165
        // query whether or not the configured source directory is available
166
        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
        throw new \Exception(sprintf('Configured source directory "%s" is not available!', $sourceDir));
175
    }
176
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
    }
210
211
    /**
212
     * 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
    }
220
221
    /**
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

src/Subjects/FileWriter/OkFileAwareFileWriter.php 1 location

@@ 38-231 (lines=194) @@
35
 * @link      https://github.com/techdivision/import
36
 * @link      http://www.techdivision.com
37
 */
38
class OkFileAwareFileWriter implements FileWriterInterface
39
{
40
41
    /**
42
     * The registry processor instance.
43
     *
44
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
45
     */
46
    private $registryProcessor;
47
48
    /**
49
     * The actual source directory to load the files from.
50
     *
51
     * @var string
52
     */
53
    private $sourceDir;
54
55
    /**
56
     * The filesystem adapter instance.
57
     *
58
     * @var \TechDivision\Import\Adapter\FilesystemAdapterInterface
59
     */
60
    private $filesystemAdapter;
61
62
    /**
63
     * The .OK file handler instance to use.
64
     *
65
     * @var \TechDivision\Import\Handlers\OkFileHandlerInterface
66
     */
67
    private $handler;
68
69
    /**
70
     * The file resolver configuration instance.
71
     *
72
     * @var \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface
73
     */
74
    private $fileResolverConfiguration;
75
76
    /**
77
     * Initializes the file resolver with the application and the registry instance.
78
     *
79
     * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry instance
80
     */
81
    public function __construct(RegistryProcessorInterface $registryProcessor)
82
    {
83
        $this->registryProcessor = $registryProcessor;
84
    }
85
86
    /**
87
     * Return's the registry processor instance.
88
     *
89
     * @return \TechDivision\Import\Services\RegistryProcessorInterface the registry processor instance
90
     */
91
    protected function getRegistryProcessor() : RegistryProcessorInterface
92
    {
93
        return $this->registryProcessor;
94
    }
95
96
    /**
97
     * Return's the .OK file handler instance.
98
     *
99
     * @return \TechDivision\Import\Handlers\OkFileHandlerInterface The .OK file handler instance
100
     */
101
    protected function getHandler() : OkFileHandlerInterface
102
    {
103
        return $this->handler;
104
    }
105
106
    /**
107
     * Return's the filesystem adapter instance.
108
     *
109
     * @return \TechDivision\Import\Adapter\FilesystemAdapterInterface The filesystem adapter instance
110
     */
111
    protected function getFilesystemAdapter() : FilesystemAdapterInterface
112
    {
113
        return $this->filesystemAdapter;
114
    }
115
116
    /**
117
     * Returns the file resolver configuration instance.
118
     *
119
     * @return \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface The configuration instance
120
     */
121
    protected function getFileResolverConfiguration() : FileResolverConfigurationInterface
122
    {
123
        return $this->fileResolverConfiguration;
124
    }
125
126
    /**
127
     * Sets the actual source directory to load the files from.
128
     *
129
     * @param string $sourceDir The actual source directory
130
     *
131
     * @return void
132
     */
133
    protected function setSourceDir(string $sourceDir) : void
134
    {
135
        $this->sourceDir = $sourceDir;
136
    }
137
138
    /**
139
     * Returns the actual source directory to load the files from.
140
     *
141
     * @return string The actual source directory
142
     */
143
    protected function getSourceDir() : string
144
    {
145
        return $this->sourceDir;
146
    }
147
148
    /**
149
     * Initializes the file resolver for the import process with the passed serial.
150
     *
151
     * @param string $serial The unique identifier of the actual import process
152
     *
153
     * @return void
154
     * @throws \Exception Is thrown if the configured source directory is not available
155
     */
156
    protected function initialize(string $serial) : void
157
    {
158
159
        // load the actual status
160
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
161
162
        // query whether or not the configured source directory is available
163
        if ($this->getFilesystemAdapter()->isDir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) {
164
            // set the source directory, if it is accessible
165
            $this->setSourceDir($sourceDir);
166
            // return immediately
167
            return;
168
        }
169
170
        // throw an exception otherwise
171
        throw new \Exception(sprintf('Configured source directory "%s" is not available!', $sourceDir));
172
    }
173
    /**
174
     * Returns the suffix for the import files.
175
     *
176
     * @return string The suffix
177
     */
178
    protected function getSuffix() : string
179
    {
180
        return $this->getFileResolverConfiguration()->getSuffix();
181
    }
182
183
    /**
184
     * Set's he .OK file handler instance.
185
     *
186
     * @param \TechDivision\Import\Handlers\OkFileHandlerInterface $handler The .OK file handler instance
187
     */
188
    public function setHandler(OkFileHandlerInterface $handler) :void
189
    {
190
        $this->handler = $handler;
191
    }
192
193
    /**
194
     * Set's the filesystem adapter instance.
195
     *
196
     * @param \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter The filesystem adapter instance
197
     */
198
    public function setFilesystemAdapter(FilesystemAdapterInterface $filesystemAdapter) : void
199
    {
200
        $this->filesystemAdapter = $filesystemAdapter;
201
    }
202
203
    /**
204
     * Set's the file resolver configuration.
205
     *
206
     * @param \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface $fileResolverConfiguration The file resolver configuration
207
     */
208
    public function setFileResolverConfiguration(FileResolverConfigurationInterface $fileResolverConfiguration) : void
209
    {
210
        $this->fileResolverConfiguration = $fileResolverConfiguration;
211
    }
212
213
    /**
214
     * Create's the .OK files for the import with the passed serial.
215
     *
216
     * @param string $serial The serial to create the .OK files for
217
     *
218
     * @return int Return's the number of created .OK files
219
     * @throws \Exception Is thrown, one of the proposed .OK files can not be created
220
     */
221
    public function createOkFiles(string $serial) : int
222
    {
223
224
        // initialize the resolver
225
        // @TODO Check if the method can not be moved to object initialization
226
        $this->initialize($serial);
227
228
        // initialize the array with the files matching the suffix found in the source directory
229
        return $this->getHandler()->createOkFiles(sprintf('%s/*.%s', $this->getSourceDir(), $this->getSuffix()));
230
    }
231
}
232