Completed
Push — 15.x ( 60340d...89878a )
by Tim
02:34
created

AbstractPlugin::getPidFilename()   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
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 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
28
/**
29
 * Abstract plugin implementation.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import
35
 * @link      http://www.techdivision.com
36
 */
37
abstract class AbstractPlugin implements PluginInterface
38
{
39
40
    /**
41
     * The appliation instance.
42
     *
43
     * @var \TechDivision\Import\ApplicationInterface
44
     */
45
    protected $application;
46
47
    /**
48
     * The plugin configuration instance.
49
     *
50
     * @var \TechDivision\Import\Configuration\PluginConfigurationInterface
51
     */
52
    protected $pluginConfiguration;
53
54
    /**
55
     * The import adapter instance.
56
     *
57
     * @var \TechDivision\Import\Adapter\ImportAdapterInterface
58
     */
59
    protected $importAdapter;
60
61
    /**
62
     * Initializes the plugin with the application instance.
63
     *
64
     * @param \TechDivision\Import\ApplicationInterface $application The application instance
65
     */
66 3
    public function __construct(ApplicationInterface $application)
67
    {
68 3
        $this->application = $application;
69 3
    }
70
71
    /**
72
     *  Set's the plugin configuration instance.
73
     *
74
     * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance
75
     *
76
     * @return void
77
     */
78
    public function setPluginConfiguration(PluginConfigurationInterface $pluginConfiguration)
79
    {
80
        $this->pluginConfiguration = $pluginConfiguration;
81
    }
82
83
    /**
84
     * Return's the plugin configuration instance.
85
     *
86
     * @return \TechDivision\Import\Configuration\PluginConfigurationInterface The plugin configuration instance
87
     */
88
    public function getPluginConfiguration()
89
    {
90
        return $this->pluginConfiguration;
91
    }
92
93
    /**
94
     * Return's the unique serial for this import process.
95
     *
96
     * @return string The unique serial
97
     */
98
    public function getSerial()
99
    {
100
        return $this->getApplication()->getSerial();
101
    }
102
103
    /**
104
     * Set's the import adapter instance.
105
     *
106
     * @param \TechDivision\Import\Adapter\ImportAdapterInterface $importAdapter The import adapter instance
107
     *
108
     * @return void
109
     */
110
    public function setImportAdapter(ImportAdapterInterface $importAdapter)
111
    {
112
        $this->importAdapter = $importAdapter;
113
    }
114
115
    /**
116
     * Return's the import adapter instance.
117
     *
118
     * @return \TechDivision\Import\Adapter\ImportAdapterInterface The import adapter instance
119
     */
120
    public function getImportAdapter()
121
    {
122
        return $this->importAdapter;
123
    }
124
125
    /**
126
     * Return's the plugin's execution context configuration.
127
     *
128
     * @return \TechDivision\Import\Configuration\ExecutionContextConfigurationInterface The execution context configuration to use
129
     */
130
    public function getExecutionContext()
131
    {
132
        return $this->getPluginConfiguration()->getExecutionContext();
133
    }
134
135
    /**
136
     * Return's the application instance.
137
     *
138
     * @return \TechDivision\Import\ApplicationInterface The application instance
139
     */
140
    protected function getApplication()
141
    {
142
        return $this->application;
143
    }
144
145
    /**
146
     * Return's the RegistryProcessor instance to handle the running threads.
147
     *
148
     * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance
149
     */
150
    protected function getRegistryProcessor()
151
    {
152
        return $this->getApplication()->getRegistryProcessor();
153
    }
154
155
    /**
156
     * Return's the import processor instance.
157
     *
158
     * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance
159
     */
160
    protected function getImportProcessor()
161
    {
162
        return $this->getApplication()->getImportProcessor();
163
    }
164
165
    /**
166
     * Return's the logger with the passed name, by default the system logger.
167
     *
168
     * @param string $name The name of the requested system logger
169
     *
170
     * @return \Psr\Log\LoggerInterface The logger instance
171
     * @throws \Exception Is thrown, if the requested logger is NOT available
172
     */
173
    protected function getSystemLogger($name = LoggerKeys::SYSTEM)
174
    {
175
        return $this->getApplication()->getSystemLogger($name);
176
    }
177
178
    /**
179
     * Query whether or not the system logger with the passed name is available.
180
     *
181
     * @param string $name The name of the requested system logger
182
     *
183
     * @return boolean TRUE if the logger with the passed name exists, else FALSE
184
     */
185
    protected function hasSystemLogger($name = LoggerKeys::SYSTEM)
186
    {
187
        return $this->getApplication()->hasSystemLogger($name);
188
    }
189
190
    /**
191
     * Return's the array with the system logger instances.
192
     *
193
     * @return array The logger instance
194
     */
195
    protected function getSystemLoggers()
196
    {
197
        return $this->getApplication()->getSystemLoggers();
198
    }
199
200
    /**
201
     * Persist the UUID of the actual import process to the PID file.
202
     *
203
     * @return void
204
     * @throws \Exception Is thrown, if the PID can not be added
205
     */
206
    protected function lock()
207
    {
208
        $this->getApplication()->lock();
209
    }
210
211
    /**
212
     * Remove's the UUID of the actual import process from the PID file.
213
     *
214
     * @return void
215
     * @throws \Exception Is thrown, if the PID can not be removed
216
     */
217
    protected function unlock()
218
    {
219
        $this->getApplication()->unlock();
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\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
            // load the factory that creates the swift mailer instance
312
            $factory = $swiftMailerConfiguration->getFactory();
313
            // create the swift mailer instance
314
            return $factory::factory($swiftMailerConfiguration);
315
        }
316
    }
317
}
318