Completed
Push — pac-89--debug-report ( 53bbe6...d250e8 )
by Tim
02:33
created

AbstractPlugin   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 288
Duplicated Lines 4.86 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 24.62%

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 6
dl 14
loc 288
ccs 16
cts 65
cp 0.2462
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A removeLineFromFile() 0 4 1
A getConfiguration() 0 4 1
A getPidFilename() 0 4 1
A getSourceDir() 0 4 1
B removeDir() 0 26 7
A __construct() 0 4 1
A setPluginConfiguration() 0 4 1
A getPluginConfiguration() 0 4 1
A getSerial() 0 4 1
A setImportAdapter() 0 4 1
A getImportAdapter() 0 4 1
A getExecutionContext() 0 4 1
A getTargetDir() 14 14 2
A getApplication() 0 4 1
A getRegistryProcessor() 0 4 1
A getImportProcessor() 0 4 1
A getSystemLogger() 0 4 1
A hasSystemLogger() 0 4 1
A getSystemLoggers() 0 4 1
A getSwiftMailer() 0 20 4

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\Plugins\AbstractPlugin
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\Plugins;
22
23
use TechDivision\Import\Utils\LoggerKeys;
24
use TechDivision\Import\ApplicationInterface;
25
use TechDivision\Import\Configuration\PluginConfigurationInterface;
26
use TechDivision\Import\Adapter\ImportAdapterInterface;
27
use TechDivision\Import\Utils\RegistryKeys;
28
use TechDivision\Import\Loggers\SwiftMailer\TransportMailerFactoryInterface;
29
30
/**
31
 * Abstract plugin 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
abstract class AbstractPlugin implements PluginInterface
40
{
41
42
    /**
43
     * The appliation instance.
44
     *
45
     * @var \TechDivision\Import\ApplicationInterface
46
     */
47
    protected $application;
48
49
    /**
50
     * The plugin configuration instance.
51
     *
52
     * @var \TechDivision\Import\Configuration\PluginConfigurationInterface
53
     */
54
    protected $pluginConfiguration;
55
56
    /**
57
     * The import adapter instance.
58
     *
59
     * @var \TechDivision\Import\Adapter\ImportAdapterInterface
60
     */
61
    protected $importAdapter;
62
63
    /**
64
     * Initializes the plugin with the application instance.
65
     *
66
     * @param \TechDivision\Import\ApplicationInterface $application The application instance
67
     */
68 3
    public function __construct(ApplicationInterface $application)
69
    {
70 3
        $this->application = $application;
71 3
    }
72
73
    /**
74
     *  Set's the plugin configuration instance.
75
     *
76
     * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance
77
     *
78
     * @return void
79
     */
80 3
    public function setPluginConfiguration(PluginConfigurationInterface $pluginConfiguration)
81
    {
82 3
        $this->pluginConfiguration = $pluginConfiguration;
83 3
    }
84
85
    /**
86
     * Return's the plugin configuration instance.
87
     *
88
     * @return \TechDivision\Import\Configuration\PluginConfigurationInterface The plugin configuration instance
89
     */
90 3
    public function getPluginConfiguration()
91
    {
92 3
        return $this->pluginConfiguration;
93
    }
94
95
    /**
96
     * Return's the unique serial for this import process.
97
     *
98
     * @return string The unique serial
99
     */
100 2
    public function getSerial()
101
    {
102 2
        return $this->getApplication()->getSerial();
103
    }
104
105
    /**
106
     * Set's the import adapter instance.
107
     *
108
     * @param \TechDivision\Import\Adapter\ImportAdapterInterface $importAdapter The import adapter instance
109
     *
110
     * @return void
111
     */
112
    public function setImportAdapter(ImportAdapterInterface $importAdapter)
113
    {
114
        $this->importAdapter = $importAdapter;
115
    }
116
117
    /**
118
     * Return's the import adapter instance.
119
     *
120
     * @return \TechDivision\Import\Adapter\ImportAdapterInterface The import adapter instance
121
     */
122
    public function getImportAdapter()
123
    {
124
        return $this->importAdapter;
125
    }
126
127
    /**
128
     * Return's the plugin's execution context configuration.
129
     *
130
     * @return \TechDivision\Import\ExecutionContextInterface The execution context configuration to use
131
     */
132
    public function getExecutionContext()
133
    {
134
        return $this->getPluginConfiguration()->getExecutionContext();
135
    }
136
137
    /**
138
     * Return's the target directory for the artefact export.
139
     *
140
     * @return string The target directory for the artefact export
141
     */
142 View Code Duplication
    public function getTargetDir()
0 ignored issues
show
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...
143
    {
144
145
        // load the status from the registry processor
146
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
147
148
        // query whether or not a target directory (mandatory) has been configured
149
        if (isset($status[RegistryKeys::TARGET_DIRECTORY])) {
150
            return $status[RegistryKeys::TARGET_DIRECTORY];
151
        }
152
153
        // throw an exception if the root category is NOT available
154
        throw new \Exception(sprintf('Can\'t find a target directory in status data for import %s', $this->getSerial()));
155
    }
156
157
    /**
158
     * Return's the application instance.
159
     *
160
     * @return \TechDivision\Import\ApplicationInterface The application instance
161
     */
162 3
    public function getApplication()
163
    {
164 3
        return $this->application;
165
    }
166
167
    /**
168
     * Return's the RegistryProcessor instance to handle the running threads.
169
     *
170
     * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance
171
     */
172 3
    protected function getRegistryProcessor()
173
    {
174 3
        return $this->getApplication()->getRegistryProcessor();
175
    }
176
177
    /**
178
     * Return's the import processor instance.
179
     *
180
     * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance
181
     */
182
    protected function getImportProcessor()
183
    {
184
        return $this->getApplication()->getImportProcessor();
185
    }
186
187
    /**
188
     * Return's the logger with the passed name, by default the system logger.
189
     *
190
     * @param string $name The name of the requested system logger
191
     *
192
     * @return \Psr\Log\LoggerInterface The logger instance
193
     * @throws \Exception Is thrown, if the requested logger is NOT available
194
     */
195 1
    protected function getSystemLogger($name = LoggerKeys::SYSTEM)
196
    {
197 1
        return $this->getApplication()->getSystemLogger($name);
198
    }
199
200
    /**
201
     * Query whether or not the system logger with the passed name is available.
202
     *
203
     * @param string $name The name of the requested system logger
204
     *
205
     * @return boolean TRUE if the logger with the passed name exists, else FALSE
206
     */
207
    protected function hasSystemLogger($name = LoggerKeys::SYSTEM)
208
    {
209
        return $this->getApplication()->hasSystemLogger($name);
210
    }
211
212
    /**
213
     * Return's the array with the system logger instances.
214
     *
215
     * @return array The logger instance
216
     */
217
    protected function getSystemLoggers()
218
    {
219
        return $this->getApplication()->getSystemLoggers();
220
    }
221
222
    /**
223
     * Remove's the passed line from the file with the passed name.
224
     *
225
     * @param string $line     The line to be removed
226
     * @param string $filename The name of the file the line has to be removed
227
     *
228
     * @return void
229
     * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed
230
     */
231
    protected function removeLineFromFile($line, $filename)
232
    {
233
        $this->getApplication()->removeLineFromFile($line, $filename);
234
    }
235
236
    /**
237
     * Return's the system configuration.
238
     *
239
     * @return \TechDivision\Import\Configuration\ConfigurationInterface The system configuration
240
     */
241
    protected function getConfiguration()
242
    {
243
        return $this->getApplication()->getConfiguration();
244
    }
245
246
    /**
247
     * Return's the PID filename to use.
248
     *
249
     * @return string The PID filename
250
     */
251
    protected function getPidFilename()
252
    {
253
        return $this->getConfiguration()->getPidFilename();
254
    }
255
256
    /**
257
     * Return's the source directory that has to be watched for new files.
258
     *
259
     * @return string The source directory
260
     */
261
    protected function getSourceDir()
262
    {
263
        return $this->getConfiguration()->getSourceDir();
264
    }
265
266
    /**
267
     * Removes the passed directory recursively.
268
     *
269
     * @param string $src Name of the directory to remove
270
     *
271
     * @return void
272
     * @throws \Exception Is thrown, if the directory can not be removed
273
     */
274
    protected function removeDir($src)
275
    {
276
277
        // open the directory
278
        $dir = opendir($src);
279
280
        // remove files/folders recursively
281
        while (false !== ($file = readdir($dir))) {
282
            if (($file != '.') && ($file != '..')) {
283
                $full = $src . '/' . $file;
284
                if (is_dir($full)) {
285
                    $this->removeDir($full);
286
                } else {
287
                    if (!unlink($full)) {
288
                        throw new \Exception(sprintf('Can\'t remove file %s', $full));
289
                    }
290
                }
291
            }
292
        }
293
294
        // close handle and remove directory itself
295
        closedir($dir);
296
        if (!rmdir($src)) {
297
            throw new \Exception(sprintf('Can\'t remove directory %s', $src));
298
        }
299
    }
300
301
    /**
302
     * Return's the configured swift mailer instance.
303
     *
304
     * @return \Swift_Mailer|null The mailer instance
305
     */
306
    protected function getSwiftMailer()
307
    {
308
309
        // the swift mailer configuration
310
        if ($swiftMailerConfiguration = $this->getPluginConfiguration()->getSwiftMailer()) {
0 ignored issues
show
Bug introduced by
The method getSwiftMailer() does not seem to exist on object<TechDivision\Impo...ConfigurationInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
311
            // create the swift mailer (factory) instance
312
            $possibleSwiftMailer = $this->getApplication()->getContainer()->get($swiftMailerConfiguration->getId());
313
314
            // query whether or not we've a factory or the instance
315
            /** @var \Swift_Mailer $swiftMailer */
316
            if ($possibleSwiftMailer instanceof TransportMailerFactoryInterface) {
317
                return $possibleSwiftMailer->factory($swiftMailerConfiguration->getTransport());
318
            } elseif ($possibleSwiftMailer instanceof \Swift_Mailer) {
319
                return $possibleSwiftMailer;
320
            }
321
322
            // throw an exeception if the configuration contains an invalid value
323
            throw new \Exception('Can\'t create SwiftMailer from configuration');
324
        }
325
    }
326
}
327