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

SubjectExecutor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 136
rs 10
c 0
b 0
f 0
ccs 0
cts 51
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B execute() 0 72 5
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 message with the subject information
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
        try {
109
            // load the subject + plug-in ID to prepare the event names
110
            $subjectName = $subject->getName();
111
            $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...
112
113
            // invoke the event that has to be fired before the subject's import method will be invoked
114
            $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...
115
            $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...
116
117
            // setup the subject instance
118
            $subjectInstance->setUp($serial);
119
120
            // initialize the callbacks/observers
121
            $this->callbackVisitor->visit($subjectInstance);
122
            $this->observerVisitor->visit($subjectInstance);
123
124
            // finally import the CSV file
125
            $subjectInstance->import($serial, $pathname);
126
127
            // query whether or not, we've to export artefacts
128
            if ($subjectInstance instanceof ExportableSubjectInterface) {
129
                try {
130
                    // invoke the event that has to be fired before the subject's export method will be invoked
131
                    $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...
132
                    $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...
133
134
                    // export the artefacts if available
135
                    $subjectInstance->export(
136
                        $matches[BunchKeys::FILENAME],
137
                        $matches[BunchKeys::COUNTER]
138
                    );
139
140
                    // invoke the event that has to be fired after the subject's export method has been invoked
141
                    $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...
142
                    $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...
143
                } catch (\Exception $e) {
144
                    // invoke the event that has to be fired when the subject's export method throws an exception
145
                    $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...
146
                    $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...
147
148
                    // re-throw the exception
149
                    throw $e;
150
                }
151
            }
152
153
            // tear down the subject instance
154
            $subjectInstance->tearDown($serial);
155
156
            // invoke the event that has to be fired after the subject's import method has been invoked
157
            $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...
158
            $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...
159
        } catch (\Exception $e) {
160
            // query whether or not, we've to export artefacts
161
            if ($subjectInstance instanceof ExportableSubjectInterface) {
162
                // tear down the subject instance
163
                $subjectInstance->tearDown($serial);
164
            }
165
166
            // invoke the event that has to be fired when the subject's import method throws an exception
167
            $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...
168
            $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...
169
170
            // re-throw the exception
171
            throw $e;
172
        }
173
    }
174
}
175