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

AbstractPlugin::getConfiguration()   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
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
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 3
    public function setPluginConfiguration(PluginConfigurationInterface $pluginConfiguration)
80
    {
81 3
        $this->pluginConfiguration = $pluginConfiguration;
82 3
    }
83
84
    /**
85
     * Return's the plugin configuration instance.
86
     *
87
     * @return \TechDivision\Import\Configuration\PluginConfigurationInterface The plugin configuration instance
88
     */
89 3
    public function getPluginConfiguration()
90
    {
91 3
        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 2
    public function getSerial()
100
    {
101 2
        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();
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 3
    public function getApplication()
162
    {
163 3
        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 3
    protected function getRegistryProcessor()
172
    {
173 3
        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 1
    protected function getSystemLogger($name = LoggerKeys::SYSTEM)
195
    {
196 1
        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
     * Remove's the passed line from the file with the passed name.
223
     *
224
     * @param string $line     The line to be removed
225
     * @param string $filename The name of the file the line has to be removed
226
     *
227
     * @return void
228
     * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed
229
     */
230
    protected function removeLineFromFile($line, $filename)
231
    {
232
        $this->getApplication()->removeLineFromFile($line, $filename);
233
    }
234
235
    /**
236
     * Return's the system configuration.
237
     *
238
     * @return \TechDivision\Import\Configuration\ConfigurationInterface The system configuration
239
     */
240
    protected function getConfiguration()
241
    {
242
        return $this->getApplication()->getConfiguration();
243
    }
244
245
    /**
246
     * Return's the PID filename to use.
247
     *
248
     * @return string The PID filename
249
     */
250
    protected function getPidFilename()
251
    {
252
        return $this->getConfiguration()->getPidFilename();
253
    }
254
255
    /**
256
     * Return's the source directory that has to be watched for new files.
257
     *
258
     * @return string The source directory
259
     */
260
    protected function getSourceDir()
261
    {
262
        return $this->getConfiguration()->getSourceDir();
263
    }
264
265
    /**
266
     * Removes the passed directory recursively.
267
     *
268
     * @param string $src Name of the directory to remove
269
     *
270
     * @return void
271
     * @throws \Exception Is thrown, if the directory can not be removed
272
     */
273
    protected function removeDir($src)
274
    {
275
276
        // open the directory
277
        $dir = opendir($src);
278
279
        // remove files/folders recursively
280
        while (false !== ($file = readdir($dir))) {
281
            if (($file != '.') && ($file != '..')) {
282
                $full = $src . '/' . $file;
283
                if (is_dir($full)) {
284
                    $this->removeDir($full);
285
                } else {
286
                    if (!unlink($full)) {
287
                        throw new \Exception(sprintf('Can\'t remove file %s', $full));
288
                    }
289
                }
290
            }
291
        }
292
293
        // close handle and remove directory itself
294
        closedir($dir);
295
        if (!rmdir($src)) {
296
            throw new \Exception(sprintf('Can\'t remove directory %s', $src));
297
        }
298
    }
299
300
    /**
301
     * Return's the configured swift mailer instance.
302
     *
303
     * @return \Swift_Mailer|null The mailer instance
304
     */
305
    protected function getSwiftMailer()
306
    {
307
308
        // the swift mailer configuration
309
        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...
310
            // load the factory that creates the swift mailer instance
311
            $factory = $swiftMailerConfiguration->getFactory();
312
            // create the swift mailer instance
313
            return $factory::factory($swiftMailerConfiguration);
314
        }
315
    }
316
}
317