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

PluginExecutor::execute()   B

Complexity

Conditions 4
Paths 30

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 8.9818
c 0
b 0
f 0
ccs 0
cts 30
cp 0
cc 4
nc 30
nop 1
crap 20

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * TechDivision\Import\Plugins\PluginExecutor
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 2019 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 League\Event\EmitterInterface;
24
use TechDivision\Import\Utils\EventNames;
25
use TechDivision\Import\Configuration\PluginConfigurationInterface;
26
27
/**
28
 * The plug-in executor instance.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2019 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
class PluginExecutor implements PluginExecutorInterface
37
{
38
39
    /**
40
     * The plug-in factory instance.
41
     *
42
     * @var \TechDivision\Import\Plugins\PluginFactoryInterface
43
     */
44
    protected $pluginFactory;
45
46
    /**
47
     * The event emitter instance.
48
     *
49
     * @var \League\Event\EmitterInterface
50
     */
51
    protected $emitter;
52
53
    /**
54
     * Initializes the plug-in executor with the application instance.
55
     *
56
     * @param \TechDivision\Import\Plugins\PluginFactoryInterface $pluginFactory  The plug-in factory instance
57
     * @param \League\Event\EmitterInterface                      $emitter        The event emitter instance
58
     */
59
    public function __construct(
60
        PluginFactoryInterface $pluginFactory,
61
        EmitterInterface $emitter
62
    ) {
63
64
        // initialize the subject factory and the event emitter
65
        $this->pluginFactory = $pluginFactory;
66
        $this->emitter = $emitter;
67
    }
68
69
    /**
70
     * Return's the event emitter instance.
71
     *
72
     * @return \League\Event\EmitterInterface The event emitter instance
73
     */
74
    protected function getEmitter()
75
    {
76
        return $this->emitter;
77
    }
78
79
    /**
80
     * Return's the plug-in factory instance.
81
     *
82
     * @return \TechDivision\Import\Plugins\PluginFactoryInterface The plug-in factory instance
83
     */
84
    protected function getPluginFactory()
85
    {
86
        return $this->pluginFactory;
87
    }
88
89
    /**
90
     * Executes the plugin with the passed configuration.
91
     *
92
     * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $plugin The message with the plugin information
93
     *
94
     * @return void
95
     */
96
    public function execute(PluginConfigurationInterface $plugin)
97
    {
98
99
        // initialize the plugin
100
        $pluginInstance = $this->getPluginFactory()->createPlugin($plugin);
101
102
        try {
103
            // load the subject + plug-in ID to prepare the event names
104
            $pluginName = $plugin->getName();
105
106
            // invoke the event that has to be fired before the plugin will be executed
107
            $this->getEmitter()->emit(EventNames::PLUGIN_PROCESS_START, $pluginInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $pluginInstance.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
108
            $this->getEmitter()->emit(sprintf('%s.%s', $pluginName, EventNames::PLUGIN_PROCESS_START), $pluginInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $pluginInstance.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
109
110
            // initialize the counter
111
            $counter = 1;
112
113
            // process the plugin
114
            $pluginInstance->process();
115
116
            // query whether or not, we've to export artefacts
117
            if ($pluginInstance instanceof ExportablePluginInterface) {
118
                try {
119
                    // invoke the event that has to be fired before the subject's export method will be invoked
120
                    $this->getEmitter()->emit(EventNames::PLUGIN_EXPORT_START, $pluginInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $pluginInstance.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
121
                    $this->getEmitter()->emit(sprintf('%s.%s', $pluginName, EventNames::PLUGIN_EXPORT_START), $pluginInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $pluginInstance.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
122
123
                    // export the plugin's artefacts if available
124
                    $pluginInstance->export(date('Ymd-His'), str_pad($counter++, 2, 0, STR_PAD_LEFT));
125
126
                    // invoke the event that has to be fired after the subject's export method has been invoked
127
                    $this->getEmitter()->emit(EventNames::PLUGIN_EXPORT_SUCCESS, $pluginInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $pluginInstance.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
128
                    $this->getEmitter()->emit(sprintf('%s.%s', $pluginName, EventNames::PLUGIN_EXPORT_SUCCESS), $pluginInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $pluginInstance.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
129
                } catch (\Exception $e) {
130
                    // invoke the event that has to be fired when the subject's export method throws an exception
131
                    $this->getEmitter()->emit(EventNames::PLUGIN_EXPORT_FAILURE, $pluginInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $pluginInstance.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
132
                    $this->getEmitter()->emit(sprintf('%s.%s', $pluginName, EventNames::PLUGIN_EXPORT_FAILURE), $pluginInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $pluginInstance.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
133
134
                    // re-throw the exception
135
                    throw $e;
136
                }
137
            }
138
139
            // invoke the event that has to be fired after the plugin has been executed
140
            $this->getEmitter()->emit(EventNames::PLUGIN_PROCESS_SUCCESS, $pluginInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $pluginInstance.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
141
            $this->getEmitter()->emit(sprintf('%s.%s', $pluginName, EventNames::PLUGIN_PROCESS_SUCCESS), $pluginInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $pluginInstance.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
142
        } catch (\Exception $e) {
143
            // invoke the event that has to be fired when the plugin throws an exception
144
            $this->getEmitter()->emit(EventNames::PLUGIN_PROCESS_FAILURE, $pluginInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $pluginInstance.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
145
            $this->getEmitter()->emit(sprintf('%s.%s', $pluginName, EventNames::PLUGIN_PROCESS_FAILURE), $pluginInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $pluginInstance.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
146
147
            // re-throw the exception
148
            throw $e;
149
        }
150
    }
151
}
152