Completed
Pull Request — master (#50)
by Tim
04:59
created

AbstractPlugin::getSystemLoggers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
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\ApplicationInterface;
24
use TechDivision\Import\Configuration\PluginConfigurationInterface;
25
use TechDivision\Import\Utils\LoggerKeys;
26
27
/**
28
 * Abstract plugin implementation.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36
abstract class AbstractPlugin implements PluginInterface
37
{
38
39
    /**
40
     * The appliation instance.
41
     *
42
     * @var \TechDivision\Import\ApplicationInterface
43
     */
44
    protected $application;
45
46
    /**
47
     * The plugin configuration instance.
48
     *
49
     * @var \TechDivision\Import\Configuration\PluginConfigurationInterface
50
     */
51
    protected $pluginConfiguration;
52
53
    /**
54
     * Initializes the plugin with the application instance.
55
     *
56
     * @param \TechDivision\Import\ApplicationInterface                       $application         The application instance
57
     * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance
58
     */
59 2
    public function __construct(
60
        ApplicationInterface $application,
61
        PluginConfigurationInterface $pluginConfiguration
62
    ) {
63 2
        $this->application = $application;
64 2
        $this->pluginConfiguration = $pluginConfiguration;
65 2
    }
66
67
    /**
68
     * Return's the application instance.
69
     *
70
     * @return \TechDivision\Import\ApplicationInterface The application instance
71
     */
72
    protected function getApplication()
73
    {
74
        return $this->application;
75
    }
76
77
    /**
78
     * Return's the plugin configuration instance.
79
     *
80
     * @return \TechDivision\Import\Configuration\PluginConfigurationInterface The plugin configuration instance
81
     */
82
    protected function getPluginConfiguration()
83
    {
84
        return $this->pluginConfiguration;
85
    }
86
87
    /**
88
     * Return's the RegistryProcessor instance to handle the running threads.
89
     *
90
     * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance
91
     */
92
    protected function getRegistryProcessor()
93
    {
94
        return $this->getApplication()->getRegistryProcessor();
95
    }
96
97
    /**
98
     * Return's the import processor instance.
99
     *
100
     * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance
101
     */
102
    protected function getImportProcessor()
103
    {
104
        return $this->getApplication()->getImportProcessor();
105
    }
106
107
    /**
108
     * Return's the unique serial for this import process.
109
     *
110
     * @return string The unique serial
111
     */
112
    protected function getSerial()
113
    {
114
        return $this->getApplication()->getSerial();
115
    }
116
117
    /**
118
     * Return's the logger with the passed name, by default the system logger.
119
     *
120
     * @param string $name The name of the requested system logger
121
     *
122
     * @return \Psr\Log\LoggerInterface The logger instance
123
     * @throws \Exception Is thrown, if the requested logger is NOT available
124
     */
125
    protected function getSystemLogger($name = LoggerKeys::SYSTEM)
126
    {
127
        return $this->getApplication()->getSystemLogger($name);
128
    }
129
130
    /**
131
     * Return's the array with the system logger instances.
132
     *
133
     * @return array The logger instance
134
     */
135
    protected function getSystemLoggers()
136
    {
137
        return $this->getApplication()->getSystemLoggers();
138
    }
139
140
    /**
141
     * Persist the UUID of the actual import process to the PID file.
142
     *
143
     * @return void
144
     * @throws \Exception Is thrown, if the PID can not be added
145
     */
146
    protected function lock()
147
    {
148
        $this->getApplication()->lock();
149
    }
150
151
    /**
152
     * Remove's the UUID of the actual import process from the PID file.
153
     *
154
     * @return void
155
     * @throws \Exception Is thrown, if the PID can not be removed
156
     */
157
    protected function unlock()
158
    {
159
        $this->getApplication()->unlock();
160
    }
161
162
    /**
163
     * Remove's the passed line from the file with the passed name.
164
     *
165
     * @param string $line     The line to be removed
166
     * @param string $filename The name of the file the line has to be removed
167
     *
168
     * @return void
169
     * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed
170
     */
171
    protected function removeLineFromFile($line, $filename)
172
    {
173
        $this->getApplication()->removeLineFromFile($line, $filename);
174
    }
175
176
    /**
177
     * Return's the system configuration.
178
     *
179
     * @return \TechDivision\Import\ConfigurationInterface The system configuration
180
     */
181
    protected function getConfiguration()
182
    {
183
        return $this->getApplication()->getConfiguration();
184
    }
185
186
    /**
187
     * Return's the PID filename to use.
188
     *
189
     * @return string The PID filename
190
     */
191
    protected function getPidFilename()
192
    {
193
        return $this->getConfiguration()->getPidFilename();
194
    }
195
196
    /**
197
     * Return's the source directory that has to be watched for new files.
198
     *
199
     * @return string The source directory
200
     */
201
    protected function getSourceDir()
202
    {
203
        return $this->getConfiguration()->getSourceDir();
204
    }
205
206
    /**
207
     * Removes the passed directory recursively.
208
     *
209
     * @param string $src Name of the directory to remove
210
     *
211
     * @return void
212
     * @throws \Exception Is thrown, if the directory can not be removed
213
     */
214
    protected function removeDir($src)
215
    {
216
217
        // open the directory
218
        $dir = opendir($src);
219
220
        // remove files/folders recursively
221
        while (false !== ($file = readdir($dir))) {
222
            if (($file != '.') && ($file != '..')) {
223
                $full = $src . '/' . $file;
224
                if (is_dir($full)) {
225
                    $this->removeDir($full);
226
                } else {
227
                    if (!unlink($full)) {
228
                        throw new \Exception(sprintf('Can\'t remove file %s', $full));
229
                    }
230
                }
231
            }
232
        }
233
234
        // close handle and remove directory itself
235
        closedir($dir);
236
        if (!rmdir($src)) {
237
            throw new \Exception(sprintf('Can\'t remove directory %s', $src));
238
        }
239
    }
240
241
    /**
242
     * Return's the configured processor instance.
243
     *
244
     * @return object The processor instance
245
     * @throws \Exception Is thrown, if no processor factory has been configured
246
     */
247
    protected function getProcessor()
248
    {
249
250
        // load the plugin configuration
251
        $pluginConfiguration = $this->getPluginConfiguration();
252
253
        // instanciate and set the processor processor, if specified
254
        if ($processorFactory = $pluginConfiguration->getProcessorFactory()) {
255
            return $processorFactory::factory(
256
                $this->getImportProcessor()->getConnection(),
257
                $pluginConfiguration
258
            );
259
        }
260
261
        // throw an exception if no processor factory has been configured
262
        throw new \Exception(sprintf('No processor factory has been specified for plugin %s', get_class($this)));
263
    }
264
}
265