Completed
Push — 9.x ( 91d8fd...2bc7fb )
by Tim
13s queued 10s
created

SubjectExecutor::execute()   B

Complexity

Conditions 5
Paths 70

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 72
c 0
b 0
f 0
ccs 0
cts 40
cp 0
rs 8.2997
cc 5
nc 70
nop 4
crap 30

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\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\Plugins;
22
23
use TechDivision\Import\Utils\BunchKeys;
24
use TechDivision\Import\Callbacks\CallbackVisitorInterface;
25
use TechDivision\Import\Observers\ObserverVisitorInterface;
26
use TechDivision\Import\Subjects\SubjectFactoryInterface;
27
use TechDivision\Import\Subjects\ExportableSubjectInterface;
28
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
29
use League\Event\EmitterInterface;
30
use TechDivision\Import\Utils\EventNames;
31
32
/**
33
 * The subject executor instance.
34
 *
35
 * @author    Tim Wagner <[email protected]>
36
 * @copyright 2017 TechDivision GmbH <[email protected]>
37
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
38
 * @link      https://github.com/techdivision/import
39
 * @link      http://www.techdivision.com
40
 */
41
class SubjectExecutor implements SubjectExecutorInterface
42
{
43
44
    /**
45
     * The subject factory instance.
46
     *
47
     * @var \TechDivision\Import\Observers\ObserverVisitorInterface
48
     */
49
    protected $observerVisitor;
50
51
    /**
52
     * The subject factory instance.
53
     *
54
     * @var \TechDivision\Import\Callbacks\CallbackVisitorInterface
55
     */
56
    protected $callbackVisitor;
57
58
    /**
59
     * The subject factory instance.
60
     *
61
     * @var \TechDivision\Import\Subjects\SubjectFactoryInterface
62
     */
63
    protected $subjectFactory;
64
65
    /**
66
     * The event emitter instance.
67
     *
68
     * @var \League\Event\EmitterInterface
69
     */
70
    protected $emitter;
71
72
    /**
73
     * Initializes the plugin with the application instance.
74
     *
75
     * @param \TechDivision\Import\Callbacks\CallbackVisitorInterface $callbackVisitor The callback visitor instance
76
     * @param \TechDivision\Import\Observers\ObserverVisitorInterface $observerVisitor The observer visitor instance
77
     * @param \TechDivision\Import\Subjects\SubjectFactoryInterface   $subjectFactory  The subject factory instance
78
     * @param \League\Event\EmitterInterface                          $emitter         The event emitter instance
79
     */
80
    public function __construct(
81
        CallbackVisitorInterface $callbackVisitor,
82
        ObserverVisitorInterface $observerVisitor,
83
        SubjectFactoryInterface $subjectFactory,
84
        EmitterInterface $emitter
85
    ) {
86
87
        // initialize the callback/observer visitors
88
        $this->callbackVisitor = $callbackVisitor;
89
        $this->observerVisitor = $observerVisitor;
90
        $this->subjectFactory = $subjectFactory;
91
        $this->emitter = $emitter;
92
    }
93
94
    /**
95
     * Executes the passed subject.
96
     *
97
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject  The message with the subject information
98
     * @param array                                                            $matches  The bunch matches
99
     * @param string                                                           $serial   The UUID of the actual import
100
     * @param string                                                           $pathname The path to the file to import
101
     *
102
     * @return void
103
     */
104
    public function execute(SubjectConfigurationInterface $subject, array $matches, $serial, $pathname)
105
    {
106
107
        // initialize the subject and import the bunch
108
        $subjectInstance = $this->subjectFactory->createSubject($subject);
109
110
        try {
111
            // load the subject + plug-in ID to prepare the event names
112
            $subjectName = $subject->getName();
113
            $pluginName = $subject->getPluginConfiguration()->getName();
0 ignored issues
show
Bug introduced by
The method getName() 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...
114
115
            // invoke the event that has to be fired before the subject's import method will be invoked
116
            $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...
117
            $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...
118
119
            // setup the subject instance
120
            $subjectInstance->setUp($serial);
121
122
            // initialize the callbacks/observers
123
            $this->callbackVisitor->visit($subjectInstance);
124
            $this->observerVisitor->visit($subjectInstance);
125
126
            // finally import the CSV file
127
            $subjectInstance->import($serial, $pathname);
128
129
            // query whether or not, we've to export artefacts
130
            if ($subjectInstance instanceof ExportableSubjectInterface) {
131
                try {
132
                    // invoke the event that has to be fired before the subject's export method will be invoked
133
                    $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...
134
                    $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...
135
136
                    // export the artefacts if available
137
                    $subjectInstance->export(
138
                        $matches[BunchKeys::FILENAME],
139
                        $matches[BunchKeys::COUNTER]
140
                    );
141
142
                    // invoke the event that has to be fired after the subject's export method has been invoked
143
                    $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...
144
                    $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...
145
                } catch (\Exception $e) {
146
                    // invoke the event that has to be fired when the subject's export method throws an exception
147
                    $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...
148
                    $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...
149
150
                    // re-throw the exception
151
                    throw $e;
152
                }
153
            }
154
155
            // tear down the subject instance
156
            $subjectInstance->tearDown($serial);
157
158
            // invoke the event that has to be fired after the subject's import method has been invoked
159
            $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...
160
            $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...
161
        } catch (\Exception $e) {
162
            // query whether or not, we've to export artefacts
163
            if ($subjectInstance instanceof ExportableSubjectInterface) {
164
                // tear down the subject instance
165
                $subjectInstance->tearDown($serial);
166
            }
167
168
            // invoke the event that has to be fired when the subject's import method throws an exception
169
            $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...
170
            $this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_FAILURE), $subjectInstance);
0 ignored issues
show
Bug introduced by
The variable $pluginName does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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...
171
172
            // re-throw the exception
173
            throw $e;
174
        }
175
    }
176
}
177