Completed
Push — master ( b5c840...3d5461 )
by Marcus
04:46
created

OkFileAwareFileWriter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 201
Duplicated Lines 8.46 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 17
loc 201
ccs 0
cts 58
cp 0
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRegistryProcessor() 0 4 1
A getHandler() 0 4 1
A getFilesystemAdapter() 0 4 1
A getFileResolverConfiguration() 0 4 1
A setSourceDir() 0 4 1
A getSourceDir() 0 4 1
A getSuffix() 0 4 1
A setHandler() 0 4 1
A setFilesystemAdapter() 0 4 1
A setFileResolverConfiguration() 0 4 1
A createOkFiles() 0 10 1
A initialize() 17 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * TechDivision\Import\Subjects\FileWriter\OkFileAwareFileWriter
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\Subjects\FileWriter;
22
23
use TechDivision\Import\Utils\RegistryKeys;
24
use TechDivision\Import\Handlers\OkFileHandlerInterface;
25
use TechDivision\Import\Adapter\FilesystemAdapterInterface;
26
use TechDivision\Import\Services\RegistryProcessorInterface;
27
use TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface;
28
29
/**
30
 * File writer implementation for .OK files.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2020 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import
36
 * @link      http://www.techdivision.com
37
 */
38
class OkFileAwareFileWriter implements OkFileAwareFileWriterInterface
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 View Code Duplication
    protected function initialize(string $serial) : void
0 ignored issues
show
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...
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...
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
    /**
175
     * Returns the suffix for the import files.
176
     *
177
     * @return string The suffix
178
     */
179
    protected function getSuffix() : string
180
    {
181
        return $this->getFileResolverConfiguration()->getSuffix();
182
    }
183
184
    /**
185
     * Set's he .OK file handler instance.
186
     *
187
     * @param \TechDivision\Import\Handlers\OkFileHandlerInterface $handler The .OK file handler instance
188
     *
189
     * @return void
190
     */
191
    public function setHandler(OkFileHandlerInterface $handler) :void
192
    {
193
        $this->handler = $handler;
194
    }
195
196
    /**
197
     * Set's the filesystem adapter instance.
198
     *
199
     * @param \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter The filesystem adapter instance
200
     *
201
     * @return void
202
     */
203
    public function setFilesystemAdapter(FilesystemAdapterInterface $filesystemAdapter) : void
204
    {
205
        $this->filesystemAdapter = $filesystemAdapter;
206
    }
207
208
    /**
209
     * Set's the file resolver configuration.
210
     *
211
     * @param \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface $fileResolverConfiguration The file resolver configuration
212
     *
213
     * @return void
214
     */
215
    public function setFileResolverConfiguration(FileResolverConfigurationInterface $fileResolverConfiguration) : void
216
    {
217
        $this->fileResolverConfiguration = $fileResolverConfiguration;
218
    }
219
220
    /**
221
     * Create's the .OK files for the import with the passed serial.
222
     *
223
     * @param string $serial The serial to create the .OK files for
224
     *
225
     * @return int Return's the number of created .OK files
226
     * @throws \Exception Is thrown, one of the proposed .OK files can not be created
227
     */
228
    public function createOkFiles(string $serial) : int
229
    {
230
231
        // initialize the resolver
232
        // @TODO Check if the method can not be moved to object initialization
233
        $this->initialize($serial);
234
235
        // initialize the array with the files matching the suffix found in the source directory
236
        return $this->getHandler()->createOkFiles(sprintf('%s/*.%s', $this->getSourceDir(), $this->getSuffix()));
237
    }
238
}
239