Completed
Push — 15.x ( 1a7f4e...53b7d0 )
by Tim
65:00 queued 61:58
created

AbstractPlugin::getSystemLoggers()   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 1
    public function setPluginConfiguration(PluginConfigurationInterface $pluginConfiguration)
79
    {
80 1
        $this->pluginConfiguration = $pluginConfiguration;
81 1
    }
82
83
    /**
84
     * Return's the plugin configuration instance.
85
     *
86
     * @return \TechDivision\Import\Configuration\PluginConfigurationInterface The plugin configuration instance
87
     */
88 1
    public function getPluginConfiguration()
89
    {
90 1
        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 application instance.
127
     *
128
     * @return \TechDivision\Import\ApplicationInterface The application instance
129
     */
130 1
    protected function getApplication()
131
    {
132 1
        return $this->application;
133
    }
134
135
    /**
136
     * Return's the RegistryProcessor instance to handle the running threads.
137
     *
138
     * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance
139
     */
140 1
    protected function getRegistryProcessor()
141
    {
142 1
        return $this->getApplication()->getRegistryProcessor();
143
    }
144
145
    /**
146
     * Return's the import processor instance.
147
     *
148
     * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance
149
     */
150
    protected function getImportProcessor()
151
    {
152
        return $this->getApplication()->getImportProcessor();
153
    }
154
155
    /**
156
     * Return's the logger with the passed name, by default the system logger.
157
     *
158
     * @param string $name The name of the requested system logger
159
     *
160
     * @return \Psr\Log\LoggerInterface The logger instance
161
     * @throws \Exception Is thrown, if the requested logger is NOT available
162
     */
163
    protected function getSystemLogger($name = LoggerKeys::SYSTEM)
164
    {
165
        return $this->getApplication()->getSystemLogger($name);
166
    }
167
168
    /**
169
     * Query whether or not the system logger with the passed name is available.
170
     *
171
     * @param string $name The name of the requested system logger
172
     *
173
     * @return boolean TRUE if the logger with the passed name exists, else FALSE
174
     */
175
    protected function hasSystemLogger($name = LoggerKeys::SYSTEM)
176
    {
177
        return $this->getApplication()->hasSystemLogger($name);
178
    }
179
180
    /**
181
     * Return's the array with the system logger instances.
182
     *
183
     * @return array The logger instance
184
     */
185
    protected function getSystemLoggers()
186
    {
187
        return $this->getApplication()->getSystemLoggers();
188
    }
189
190
    /**
191
     * Persist the UUID of the actual import process to the PID file.
192
     *
193
     * @return void
194
     * @throws \Exception Is thrown, if the PID can not be added
195
     */
196
    protected function lock()
197
    {
198
        $this->getApplication()->lock();
199
    }
200
201
    /**
202
     * Remove's the UUID of the actual import process from the PID file.
203
     *
204
     * @return void
205
     * @throws \Exception Is thrown, if the PID can not be removed
206
     */
207
    protected function unlock()
208
    {
209
        $this->getApplication()->unlock();
210
    }
211
212
    /**
213
     * Remove's the passed line from the file with the passed name.
214
     *
215
     * @param string $line     The line to be removed
216
     * @param string $filename The name of the file the line has to be removed
217
     *
218
     * @return void
219
     * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed
220
     */
221
    protected function removeLineFromFile($line, $filename)
222
    {
223
        $this->getApplication()->removeLineFromFile($line, $filename);
224
    }
225
226
    /**
227
     * Return's the system configuration.
228
     *
229
     * @return \TechDivision\Import\ConfigurationInterface The system configuration
230
     */
231 1
    protected function getConfiguration()
232
    {
233 1
        return $this->getApplication()->getConfiguration();
234
    }
235
236
    /**
237
     * Return's the PID filename to use.
238
     *
239
     * @return string The PID filename
240
     */
241
    protected function getPidFilename()
242
    {
243
        return $this->getConfiguration()->getPidFilename();
244
    }
245
246
    /**
247
     * Return's the source directory that has to be watched for new files.
248
     *
249
     * @return string The source directory
250
     */
251
    protected function getSourceDir()
252
    {
253
        return $this->getConfiguration()->getSourceDir();
254
    }
255
256
    /**
257
     * Removes the passed directory recursively.
258
     *
259
     * @param string $src Name of the directory to remove
260
     *
261
     * @return void
262
     * @throws \Exception Is thrown, if the directory can not be removed
263
     */
264
    protected function removeDir($src)
265
    {
266
267
        // open the directory
268
        $dir = opendir($src);
269
270
        // remove files/folders recursively
271
        while (false !== ($file = readdir($dir))) {
272
            if (($file != '.') && ($file != '..')) {
273
                $full = $src . '/' . $file;
274
                if (is_dir($full)) {
275
                    $this->removeDir($full);
276
                } else {
277
                    if (!unlink($full)) {
278
                        throw new \Exception(sprintf('Can\'t remove file %s', $full));
279
                    }
280
                }
281
            }
282
        }
283
284
        // close handle and remove directory itself
285
        closedir($dir);
286
        if (!rmdir($src)) {
287
            throw new \Exception(sprintf('Can\'t remove directory %s', $src));
288
        }
289
    }
290
291
    /**
292
     * Return's the configured swift mailer instance.
293
     *
294
     * @return \Swift_Mailer|null The mailer instance
295
     */
296
    protected function getSwiftMailer()
297
    {
298
299
        // the swift mailer configuration
300
        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...
301
            // load the factory that creates the swift mailer instance
302
            $factory = $swiftMailerConfiguration->getFactory();
303
            // create the swift mailer instance
304
            return $factory::factory($swiftMailerConfiguration);
305
        }
306
    }
307
}
308