Completed
Push — master ( fa762a...3ffd9d )
by Tim
9s
created

AbstractPlugin::hasSystemLogger()   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 1
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
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
     * Query whether or not the system logger with the passed name is available.
132
     *
133
     * @param string $name The name of the requested system logger
134
     *
135
     * @return boolean TRUE if the logger with the passed name exists, else FALSE
136
     */
137
    protected function hasSystemLogger($name = LoggerKeys::SYSTEM)
138
    {
139
        return $this->getApplication()->hasSystemLogger($name);
140
    }
141
142
    /**
143
     * Return's the array with the system logger instances.
144
     *
145
     * @return array The logger instance
146
     */
147
    protected function getSystemLoggers()
148
    {
149
        return $this->getApplication()->getSystemLoggers();
150
    }
151
152
    /**
153
     * Persist the UUID of the actual import process to the PID file.
154
     *
155
     * @return void
156
     * @throws \Exception Is thrown, if the PID can not be added
157
     */
158
    protected function lock()
159
    {
160
        $this->getApplication()->lock();
161
    }
162
163
    /**
164
     * Remove's the UUID of the actual import process from the PID file.
165
     *
166
     * @return void
167
     * @throws \Exception Is thrown, if the PID can not be removed
168
     */
169
    protected function unlock()
170
    {
171
        $this->getApplication()->unlock();
172
    }
173
174
    /**
175
     * Remove's the passed line from the file with the passed name.
176
     *
177
     * @param string $line     The line to be removed
178
     * @param string $filename The name of the file the line has to be removed
179
     *
180
     * @return void
181
     * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed
182
     */
183
    protected function removeLineFromFile($line, $filename)
184
    {
185
        $this->getApplication()->removeLineFromFile($line, $filename);
186
    }
187
188
    /**
189
     * Return's the system configuration.
190
     *
191
     * @return \TechDivision\Import\ConfigurationInterface The system configuration
192
     */
193
    protected function getConfiguration()
194
    {
195
        return $this->getApplication()->getConfiguration();
196
    }
197
198
    /**
199
     * Return's the PID filename to use.
200
     *
201
     * @return string The PID filename
202
     */
203
    protected function getPidFilename()
204
    {
205
        return $this->getConfiguration()->getPidFilename();
206
    }
207
208
    /**
209
     * Return's the source directory that has to be watched for new files.
210
     *
211
     * @return string The source directory
212
     */
213
    protected function getSourceDir()
214
    {
215
        return $this->getConfiguration()->getSourceDir();
216
    }
217
218
    /**
219
     * Removes the passed directory recursively.
220
     *
221
     * @param string $src Name of the directory to remove
222
     *
223
     * @return void
224
     * @throws \Exception Is thrown, if the directory can not be removed
225
     */
226
    protected function removeDir($src)
227
    {
228
229
        // open the directory
230
        $dir = opendir($src);
231
232
        // remove files/folders recursively
233
        while (false !== ($file = readdir($dir))) {
234
            if (($file != '.') && ($file != '..')) {
235
                $full = $src . '/' . $file;
236
                if (is_dir($full)) {
237
                    $this->removeDir($full);
238
                } else {
239
                    if (!unlink($full)) {
240
                        throw new \Exception(sprintf('Can\'t remove file %s', $full));
241
                    }
242
                }
243
            }
244
        }
245
246
        // close handle and remove directory itself
247
        closedir($dir);
248
        if (!rmdir($src)) {
249
            throw new \Exception(sprintf('Can\'t remove directory %s', $src));
250
        }
251
    }
252
253
    /**
254
     * Return's the configured swift mailer instance.
255
     *
256
     * @return \Swift_Mailer|null The mailer instance
257
     */
258
    protected function getSwiftMailer()
259
    {
260
261
        // the swift mailer configuration
262
        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...
263
            // load the factory that creates the swift mailer instance
264
            $factory = $swiftMailerConfiguration->getFactory();
265
            // create the swift mailer instance
266
            return $factory::factory($swiftMailerConfiguration);
267
        }
268
    }
269
}
270