Completed
Push — 15.x ( 2aa3b6...167fc6 )
by Tim
02:35
created

AbstractPlugin::getImportAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
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
29
/**
30
 * Abstract plugin implementation.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2016 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
abstract class AbstractPlugin implements PluginInterface
39
{
40
41
    /**
42
     * The appliation instance.
43
     *
44
     * @var \TechDivision\Import\ApplicationInterface
45
     */
46
    protected $application;
47
48
    /**
49
     * The plugin configuration instance.
50
     *
51
     * @var \TechDivision\Import\Configuration\PluginConfigurationInterface
52
     */
53
    protected $pluginConfiguration;
54
55
    /**
56
     * The import adapter instance.
57
     *
58
     * @var \TechDivision\Import\Adapter\ImportAdapterInterface
59
     */
60
    protected $importAdapter;
61
62
    /**
63
     * Initializes the plugin with the application instance.
64
     *
65
     * @param \TechDivision\Import\ApplicationInterface $application The application instance
66
     */
67 3
    public function __construct(ApplicationInterface $application)
68
    {
69 3
        $this->application = $application;
70 3
    }
71
72
    /**
73
     *  Set's the plugin configuration instance.
74
     *
75
     * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance
76
     *
77
     * @return void
78
     */
79
    public function setPluginConfiguration(PluginConfigurationInterface $pluginConfiguration)
80
    {
81
        $this->pluginConfiguration = $pluginConfiguration;
82
    }
83
84
    /**
85
     * Return's the plugin configuration instance.
86
     *
87
     * @return \TechDivision\Import\Configuration\PluginConfigurationInterface The plugin configuration instance
88
     */
89
    public function getPluginConfiguration()
90
    {
91
        return $this->pluginConfiguration;
92
    }
93
94
    /**
95
     * Return's the unique serial for this import process.
96
     *
97
     * @return string The unique serial
98
     */
99
    public function getSerial()
100
    {
101
        return $this->getApplication()->getSerial();
102
    }
103
104
    /**
105
     * Set's the import adapter instance.
106
     *
107
     * @param \TechDivision\Import\Adapter\ImportAdapterInterface $importAdapter The import adapter instance
108
     *
109
     * @return void
110
     */
111
    public function setImportAdapter(ImportAdapterInterface $importAdapter)
112
    {
113
        $this->importAdapter = $importAdapter;
114
    }
115
116
    /**
117
     * Return's the import adapter instance.
118
     *
119
     * @return \TechDivision\Import\Adapter\ImportAdapterInterface The import adapter instance
120
     */
121
    public function getImportAdapter()
122
    {
123
        return $this->importAdapter;
124
    }
125
126
    /**
127
     * Return's the plugin's execution context configuration.
128
     *
129
     * @return \TechDivision\Import\ExecutionContextInterface The execution context configuration to use
130
     */
131
    public function getExecutionContext()
132
    {
133
        return $this->getPluginConfiguration()->getExecutionContext();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getPluginC...>getExecutionContext(); (TechDivision\Import\ExecutionContextInterface) is incompatible with the return type declared by the interface TechDivision\Import\Plug...ce::getExecutionContext of type TechDivision\Import\Conf...tConfigurationInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
134
    }
135
136
    /**
137
     * Return's the target directory for the artefact export.
138
     *
139
     * @return string The target directory for the artefact export
140
     */
141 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...
142
    {
143
144
        // load the status from the registry processor
145
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
146
147
        // query whether or not a target directory (mandatory) has been configured
148
        if (isset($status[RegistryKeys::TARGET_DIRECTORY])) {
149
            return $status[RegistryKeys::TARGET_DIRECTORY];
150
        }
151
152
        // throw an exception if the root category is NOT available
153
        throw new \Exception(sprintf('Can\'t find a target directory in status data for import %s', $this->getSerial()));
154
    }
155
156
    /**
157
     * Return's the application instance.
158
     *
159
     * @return \TechDivision\Import\ApplicationInterface The application instance
160
     */
161
    protected function getApplication()
162
    {
163
        return $this->application;
164
    }
165
166
    /**
167
     * Return's the RegistryProcessor instance to handle the running threads.
168
     *
169
     * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance
170
     */
171
    protected function getRegistryProcessor()
172
    {
173
        return $this->getApplication()->getRegistryProcessor();
174
    }
175
176
    /**
177
     * Return's the import processor instance.
178
     *
179
     * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance
180
     */
181
    protected function getImportProcessor()
182
    {
183
        return $this->getApplication()->getImportProcessor();
184
    }
185
186
    /**
187
     * Return's the logger with the passed name, by default the system logger.
188
     *
189
     * @param string $name The name of the requested system logger
190
     *
191
     * @return \Psr\Log\LoggerInterface The logger instance
192
     * @throws \Exception Is thrown, if the requested logger is NOT available
193
     */
194
    protected function getSystemLogger($name = LoggerKeys::SYSTEM)
195
    {
196
        return $this->getApplication()->getSystemLogger($name);
197
    }
198
199
    /**
200
     * Query whether or not the system logger with the passed name is available.
201
     *
202
     * @param string $name The name of the requested system logger
203
     *
204
     * @return boolean TRUE if the logger with the passed name exists, else FALSE
205
     */
206
    protected function hasSystemLogger($name = LoggerKeys::SYSTEM)
207
    {
208
        return $this->getApplication()->hasSystemLogger($name);
209
    }
210
211
    /**
212
     * Return's the array with the system logger instances.
213
     *
214
     * @return array The logger instance
215
     */
216
    protected function getSystemLoggers()
217
    {
218
        return $this->getApplication()->getSystemLoggers();
219
    }
220
221
    /**
222
     * Persist the UUID of the actual import process to the PID file.
223
     *
224
     * @return void
225
     * @throws \Exception Is thrown, if the PID can not be added
226
     */
227
    protected function lock()
228
    {
229
        $this->getApplication()->lock();
230
    }
231
232
    /**
233
     * Remove's the UUID of the actual import process from the PID file.
234
     *
235
     * @return void
236
     * @throws \Exception Is thrown, if the PID can not be removed
237
     */
238
    protected function unlock()
239
    {
240
        $this->getApplication()->unlock();
241
    }
242
243
    /**
244
     * Remove's the passed line from the file with the passed name.
245
     *
246
     * @param string $line     The line to be removed
247
     * @param string $filename The name of the file the line has to be removed
248
     *
249
     * @return void
250
     * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed
251
     */
252
    protected function removeLineFromFile($line, $filename)
253
    {
254
        $this->getApplication()->removeLineFromFile($line, $filename);
255
    }
256
257
    /**
258
     * Return's the system configuration.
259
     *
260
     * @return \TechDivision\Import\ConfigurationInterface The system configuration
261
     */
262
    protected function getConfiguration()
263
    {
264
        return $this->getApplication()->getConfiguration();
265
    }
266
267
    /**
268
     * Return's the PID filename to use.
269
     *
270
     * @return string The PID filename
271
     */
272
    protected function getPidFilename()
273
    {
274
        return $this->getConfiguration()->getPidFilename();
275
    }
276
277
    /**
278
     * Return's the source directory that has to be watched for new files.
279
     *
280
     * @return string The source directory
281
     */
282
    protected function getSourceDir()
283
    {
284
        return $this->getConfiguration()->getSourceDir();
285
    }
286
287
    /**
288
     * Removes the passed directory recursively.
289
     *
290
     * @param string $src Name of the directory to remove
291
     *
292
     * @return void
293
     * @throws \Exception Is thrown, if the directory can not be removed
294
     */
295 View Code Duplication
    protected function removeDir($src)
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...
296
    {
297
298
        // open the directory
299
        $dir = opendir($src);
300
301
        // remove files/folders recursively
302
        while (false !== ($file = readdir($dir))) {
303
            if (($file != '.') && ($file != '..')) {
304
                $full = $src . '/' . $file;
305
                if (is_dir($full)) {
306
                    $this->removeDir($full);
307
                } else {
308
                    if (!unlink($full)) {
309
                        throw new \Exception(sprintf('Can\'t remove file %s', $full));
310
                    }
311
                }
312
            }
313
        }
314
315
        // close handle and remove directory itself
316
        closedir($dir);
317
        if (!rmdir($src)) {
318
            throw new \Exception(sprintf('Can\'t remove directory %s', $src));
319
        }
320
    }
321
322
    /**
323
     * Return's the configured swift mailer instance.
324
     *
325
     * @return \Swift_Mailer|null The mailer instance
326
     */
327
    protected function getSwiftMailer()
328
    {
329
330
        // the swift mailer configuration
331
        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...
332
            // load the factory that creates the swift mailer instance
333
            $factory = $swiftMailerConfiguration->getFactory();
334
            // create the swift mailer instance
335
            return $factory::factory($swiftMailerConfiguration);
336
        }
337
    }
338
}
339