Completed
Pull Request — master (#48)
by Tim
03:15
created

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