Completed
Push — 15.x ( 2aa3b6...167fc6 )
by Tim
02:35
created

SubjectPlugin   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 13.51%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 10
dl 0
loc 152
ccs 5
cts 37
cp 0.1351
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B process() 0 46 5
A processSubject() 0 34 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Plugins\SubjectPlugin
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\RegistryKeys;
24
use TechDivision\Import\ApplicationInterface;
25
use TechDivision\Import\Exceptions\MissingOkFileException;
26
use TechDivision\Import\Subjects\SubjectExecutorInterface;
27
use TechDivision\Import\Subjects\FileResolver\FileResolverFactoryInterface;
28
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
29
30
/**
31
 * Plugin that processes the subjects.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2016 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 SubjectPlugin extends AbstractPlugin implements SubjectAwarePluginInterface
40
{
41
42
    /**
43
     * The matches for the last processed CSV filename.
44
     *
45
     * @var array
46
     */
47
    protected $matches = array();
48
49
    /**
50
     * The number of imported bunches.
51
     *
52
     * @var integer
53
     */
54
    protected $bunches = 0;
55
56
    /**
57
     * The subject executor instance.
58
     *
59
     * @var \TechDivision\Import\Subjects\SubjectExecutorInterface
60
     */
61
    protected $subjectExecutor;
62
63
    /**
64
     * The file resolver factory instance.
65
     *
66
     * @var \TechDivision\Import\Subjects\FileResolver\FileResolverFactoryInterface
67
     */
68
    protected $fileResolverFactory;
69
70
    /**
71
     * Initializes the plugin with the application instance.
72
     *
73
     * @param \TechDivision\Import\ApplicationInterface                               $application         The application instance
74
     * @param \TechDivision\Import\Subjects\SubjectExecutorInterface                  $subjectExecutor     The subject executor instance
75
     * @param \TechDivision\Import\Subjects\FileResolver\FileResolverFactoryInterface $fileResolverFactory The file resolver instance
76
     */
77 3
    public function __construct(
78
        ApplicationInterface $application,
79
        SubjectExecutorInterface $subjectExecutor,
80
        FileResolverFactoryInterface $fileResolverFactory
81
    ) {
82
83
        // call the parent constructor
84 3
        parent::__construct($application);
85
86
        // set the subject executor and the file resolver factory
87 3
        $this->subjectExecutor = $subjectExecutor;
88 3
        $this->fileResolverFactory = $fileResolverFactory;
89 3
    }
90
91
92
    /**
93
     * Process the plugin functionality.
94
     *
95
     * @return void
96
     * @throws \Exception Is thrown, if the plugin can not be processed
97
     */
98
    public function process()
99
    {
100
101
        try {
102
            // immediately add the PID to lock this import process
103
            $this->lock();
104
105
            // load the plugin's subjects
106
            $subjects = $this->getPluginConfiguration()->getSubjects();
107
108
            // initialize the array for the status
109
            $status = array();
110
111
            // initialize the status information for the subjects
112
            /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */
113
            foreach ($subjects as $subject) {
114
                $status[$subject->getPrefix()] = array();
115
            }
116
117
            // and update it in the registry
118
            $this->getRegistryProcessor()->mergeAttributesRecursive(RegistryKeys::STATUS, $status);
119
120
            // process all the subjects found in the system configuration
121
            /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */
122
            foreach ($subjects as $subject) {
123
                $this->processSubject($subject);
124
            }
125
126
            // update the number of imported bunches
127
            $this->getRegistryProcessor()->mergeAttributesRecursive(
128
                RegistryKeys::STATUS,
129
                array(RegistryKeys::BUNCHES => $this->bunches)
130
            );
131
132
        } catch (MissingOkFileException $mofe) {
133
            // stop the application if we can't find the mandatory OK file
134
            $this->getApplication()->stop($mofe->getMessage());
135
        } catch (\Exception $e) {
136
            // re-throw the exception
137
            throw $e;
138
        } finally {
139
            // finally, if a PID has been set, remove it
140
            // from the PID file to unlock the importer
141
            $this->unlock();
142
        }
143
    }
144
145
    /**
146
     * Process the subject with the passed name/identifier.
147
     *
148
     * We create a new, fresh and separate subject for EVERY file here, because this would be
149
     * the starting point to parallelize the import process in a multithreaded/multiprocessed
150
     * environment.
151
     *
152
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The subject configuration
153
     *
154
     * @return void
155
     */
156
    protected function processSubject(SubjectConfigurationInterface $subject)
157
    {
158
159
        // initialize the bunch number
160
        $bunches = 0;
161
162
        // load the file resolver for the subject with the passed configuration
163
        $fileResolver = $this->fileResolverFactory->createFileResolver($subject);
164
165
        // load the files
166
        $files = $fileResolver->loadFiles($serial = $this->getSerial());
167
168
        // load the matches (must match the number of the found files)
169
        $matches = $fileResolver->getMatches();
170
171
        // iterate through all CSV files and process the subjects
172
        foreach ($files as $key => $filename) {
173
            // initialize the subject and import the bunch
174
            $this->subjectExecutor->execute($subject, $matches[$key], $serial, $filename);
175
            // raise the number of the imported bunches
176
            $bunches++;
177
        }
178
179
        // raise the bunch number by the imported bunches
180
        $this->bunches = $this->bunches + $bunches;
181
182
        // reset the file resolver for making it ready parsing the files of the next subject
183
        $fileResolver->reset();
184
185
        // and and log a message that the subject has been processed
186
        $this->getSystemLogger()->debug(
187
            sprintf('Successfully processed subject %s with %d bunches)!', $subject->getId(), $bunches)
188
        );
189
    }
190
}
191