Completed
Pull Request — master (#128)
by Tim
04:34
created

SubjectExecutor::execute()   B

Complexity

Conditions 4
Paths 16

Size

Total Lines 39
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 22
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 16
nop 4
crap 20
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
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
     * Initializes the plugin with the application instance.
65
     *
66
     * @param \TechDivision\Import\Callbacks\CallbackVisitorInterface $callbackVisitor The callback visitor instance
67
     * @param \TechDivision\Import\Observers\ObserverVisitorInterface $observerVisitor The observer visitor instance
68
     * @param \TechDivision\Import\Subjects\SubjectFactoryInterface   $subjectFactory  The subject factory instance
69
     */
70
    public function __construct(
71
        CallbackVisitorInterface $callbackVisitor,
72
        ObserverVisitorInterface $observerVisitor,
73
        SubjectFactoryInterface $subjectFactory
74
    ) {
75
76
        // initialize the callback/observer visitors
77
        $this->callbackVisitor = $callbackVisitor;
78
        $this->observerVisitor = $observerVisitor;
79
        $this->subjectFactory = $subjectFactory;
80
    }
81
82
    /**
83
     * Executes the passed subject.
84
     *
85
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject  The message with the subject information
86
     * @param array                                                            $matches  The bunch matches
87
     * @param string                                                           $serial   The UUID of the actual import
88
     * @param string                                                           $pathname The path to the file to import
89
     *
90
     * @return void
91
     */
92
    public function execute(SubjectConfigurationInterface $subject, array $matches, $serial, $pathname)
93
    {
94
95
        // initialize the subject and import the bunch
96
        $subjectInstance = $this->subjectFactory->createSubject($subject);
97
98
        try {
99
            // setup the subject instance
100
            $subjectInstance->setUp($serial);
101
102
            // initialize the callbacks/observers
103
            $this->callbackVisitor->visit($subjectInstance);
104
            $this->observerVisitor->visit($subjectInstance);
105
106
            // finally import the CSV file
107
            $subjectInstance->import($serial, $pathname);
108
109
            // query whether or not, we've to export artefacts
110
            if ($subjectInstance instanceof ExportableSubjectInterface) {
111
                $subjectInstance->export(
112
                    $matches[BunchKeys::FILENAME],
113
                    $matches[BunchKeys::COUNTER]
114
                );
115
            }
116
117
            // tear down the subject instance
118
            $subjectInstance->tearDown($serial);
119
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
120
        } catch (\Exception $e) {
121
            // query whether or not, we've to export artefacts
122
            if ($subjectInstance instanceof ExportableSubjectInterface) {
123
                // tear down the subject instance
124
                $subjectInstance->tearDown($serial);
125
            }
126
127
            // re-throw the exception
128
            throw $e;
129
        }
130
    }
131
}
132