Completed
Push — master ( b5c840...3d5461 )
by Marcus
04:46
created

SubjectExecutor::execute()   B

Complexity

Conditions 4
Paths 52

Size

Total Lines 77

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 0
cts 44
cp 0
rs 8.5018
c 0
b 0
f 0
cc 4
nc 52
nop 4
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\Subjects\SubjectExecutor
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\Subjects;
22
23
use League\Event\EmitterInterface;
24
use TechDivision\Import\Utils\BunchKeys;
25
use TechDivision\Import\Utils\EventNames;
26
use TechDivision\Import\Callbacks\CallbackVisitorInterface;
27
use TechDivision\Import\Observers\ObserverVisitorInterface;
28
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
29
30
/**
31
 * The subject executor instance.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2017 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import
37
 * @link      http://www.techdivision.com
38
 */
39
class SubjectExecutor implements SubjectExecutorInterface
40
{
41
42
    /**
43
     * The subject factory instance.
44
     *
45
     * @var \TechDivision\Import\Observers\ObserverVisitorInterface
46
     */
47
    protected $observerVisitor;
48
49
    /**
50
     * The subject factory instance.
51
     *
52
     * @var \TechDivision\Import\Callbacks\CallbackVisitorInterface
53
     */
54
    protected $callbackVisitor;
55
56
    /**
57
     * The subject factory instance.
58
     *
59
     * @var \TechDivision\Import\Subjects\SubjectFactoryInterface
60
     */
61
    protected $subjectFactory;
62
63
    /**
64
     * The event emitter instance.
65
     *
66
     * @var \League\Event\EmitterInterface
67
     */
68
    protected $emitter;
69
70
    /**
71
     * Initializes the plugin with the application instance.
72
     *
73
     * @param \TechDivision\Import\Callbacks\CallbackVisitorInterface $callbackVisitor The callback visitor instance
74
     * @param \TechDivision\Import\Observers\ObserverVisitorInterface $observerVisitor The observer visitor instance
75
     * @param \TechDivision\Import\Subjects\SubjectFactoryInterface   $subjectFactory  The subject factory instance
76
     * @param \League\Event\EmitterInterface                          $emitter         The event emitter instance
77
     */
78
    public function __construct(
79
        CallbackVisitorInterface $callbackVisitor,
80
        ObserverVisitorInterface $observerVisitor,
81
        SubjectFactoryInterface $subjectFactory,
82
        EmitterInterface $emitter
83
    ) {
84
85
        // initialize the callback/observer visitors
86
        $this->callbackVisitor = $callbackVisitor;
87
        $this->observerVisitor = $observerVisitor;
88
        $this->subjectFactory = $subjectFactory;
89
        $this->emitter = $emitter;
90
    }
91
92
    /**
93
     * Executes the passed subject.
94
     *
95
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject  The subject configuration instance
96
     * @param array                                                            $matches  The bunch matches
97
     * @param string                                                           $serial   The UUID of the actual import
98
     * @param string                                                           $pathname The path to the file to import
99
     *
100
     * @return void
101
     */
102
    public function execute(SubjectConfigurationInterface $subject, array $matches, $serial, $pathname)
103
    {
104
105
        // initialize the subject and import the bunch
106
        $subjectInstance = $this->subjectFactory->createSubject($subject);
107
108
        // load the plug-in an operation configuartion
109
        $pluginConfiguration = $subject->getPluginConfiguration();
110
        $operationConfiguration = $pluginConfiguration->getOperationConfiguration();
111
112
        // load the subject + plug-in ID  as well as th operation name to prepare the events
113
        $subjectName = $subject->getName();
114
        $pluginName = $pluginConfiguration->getName();
115
        $operationName = $operationConfiguration->getName();
116
117
        try {
118
            // invoke the event that has to be fired before the subject's import method will be invoked
119
            $this->emitter->emit(EventNames::SUBJECT_IMPORT_START, $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
120
            $this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_START), $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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->emitter->emit(sprintf('%s.%s.%s.%s', $operationName, $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_START), $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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
            // setup the subject instance
124
            $subjectInstance->setUp($serial);
125
126
            // initialize the callbacks/observers
127
            $this->callbackVisitor->visit($subjectInstance);
128
            $this->observerVisitor->visit($subjectInstance);
129
130
            // finally import the CSV file
131
            $subjectInstance->import($serial, $pathname);
132
133
            // query whether or not, we've to export artefacts
134
            if ($subjectInstance instanceof ExportableSubjectInterface) {
135
                try {
136
                    // invoke the event that has to be fired before the subject's export method will be invoked
137
                    $this->emitter->emit(EventNames::SUBJECT_EXPORT_START, $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
138
                    $this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_START), $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
139
                    $this->emitter->emit(sprintf('%s.%s.%s.%s', $operationName, $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_START), $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
140
141
                    // export the artefacts if available
142
                    $subjectInstance->export($matches[BunchKeys::FILENAME], $matches[BunchKeys::COUNTER]);
143
144
                    // invoke the event that has to be fired after the subject's export method has been invoked
145
                    $this->emitter->emit(EventNames::SUBJECT_EXPORT_SUCCESS, $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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
                    $this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_SUCCESS), $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
147
                    $this->emitter->emit(sprintf('%s.%s.%s.%s', $operationName, $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_SUCCESS), $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
148
                } catch (\Exception $e) {
149
                    // invoke the event that has to be fired when the subject's export method throws an exception
150
                    $this->emitter->emit(EventNames::SUBJECT_EXPORT_FAILURE, $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
151
                    $this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_FAILURE), $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
152
                    $this->emitter->emit(sprintf('%s.%s.%s.%s', $operationName, $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_FAILURE), $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
153
154
                    // re-throw the exception
155
                    throw $e;
156
                }
157
            }
158
159
            // tear down the subject instance
160
            $subjectInstance->tearDown($serial);
161
162
            // invoke the event that has to be fired after the subject's import method has been invoked
163
            $this->emitter->emit(EventNames::SUBJECT_IMPORT_SUCCESS, $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
164
            $this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_SUCCESS), $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
165
            $this->emitter->emit(sprintf('%s.%s.%s.%s', $operationName, $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_SUCCESS), $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
166
        } catch (\Exception $e) {
167
            // tear down the subject instance
168
            $subjectInstance->tearDown($serial);
169
170
            // invoke the event that has to be fired when the subject's import method throws an exception
171
            $this->emitter->emit(EventNames::SUBJECT_IMPORT_FAILURE, $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
172
            $this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_FAILURE), $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
173
            $this->emitter->emit(sprintf('%s.%s.%s.%s', $operationName, $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_FAILURE), $subjectInstance);
0 ignored issues
show
Unused Code introduced by
The call to EmitterInterface::emit() has too many arguments starting with $subjectInstance.

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...
174
175
            // re-throw the exception
176
            throw $e;
177
        }
178
    }
179
}
180