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

SubjectPlugin::process()   B

Complexity

Conditions 5
Paths 28

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0061

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 15
cts 16
cp 0.9375
rs 8.9688
c 0
b 0
f 0
cc 5
nc 28
nop 0
crap 5.0061
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 3
    public function process()
99
    {
100
101
        try {
102
            // load the plugin's subjects
103 3
            $subjects = $this->getPluginConfiguration()->getSubjects();
104
105
            // initialize the array for the status
106 3
            $status = array();
107
108
            // initialize the status information for the subjects
109
            /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */
110 3
            foreach ($subjects as $subject) {
111 2
                $status[$subject->getPrefix()] = array();
112
            }
113
114
            // and update it in the registry
115 3
            $this->getRegistryProcessor()->mergeAttributesRecursive(RegistryKeys::STATUS, $status);
116
117
            // process all the subjects found in the system configuration
118
            /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */
119 3
            foreach ($subjects as $subject) {
120 2
                $this->processSubject($subject);
121
            }
122
123
            // update the number of imported bunches
124 2
            $this->getRegistryProcessor()->mergeAttributesRecursive(
125 2
                RegistryKeys::STATUS,
126
                array(
127 2
                    RegistryKeys::BUNCHES => $this->bunches
128
                )
129
            );
130 1
        } catch (MissingOkFileException $mofe) {
131
            // stop the application if we can't find the mandatory OK file
132
            $this->getApplication()->stop($mofe->getMessage());
133 1
        } catch (\Exception $e) {
134
            // re-throw the exception
135 1
            throw $e;
136
        }
137 2
    }
138
139
    /**
140
     * Process the subject with the passed name/identifier.
141
     *
142
     * We create a new, fresh and separate subject for EVERY file here, because this would be
143
     * the starting point to parallelize the import process in a multithreaded/multiprocessed
144
     * environment.
145
     *
146
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The subject configuration
147
     *
148
     * @return void
149
     */
150 2
    protected function processSubject(SubjectConfigurationInterface $subject)
151
    {
152
153
        // initialize the file counter
154 2
        $counter = 0;
155
156
        // load the file resolver for the subject with the passed configuration
157 2
        $fileResolver = $this->fileResolverFactory->createFileResolver($subject);
158
159
        // load the files
160 2
        $files = $fileResolver->loadFiles($serial = $this->getSerial());
161
162
        // load the matches (must match the number of the found files)
163 2
        $matches = $fileResolver->getMatches();
164
165
        // iterate through all CSV files and process the subjects
166 2
        foreach ($files as $filename) {
167
            // initialize the subject and import the bunch
168 2
            $this->subjectExecutor->execute($subject, $matches[$counter], $serial, $filename);
169
            // raise the number of the imported files
170 1
            $counter++;
171
        }
172
173
        // raise the bunch number by the number of imported files
174 1
        $this->bunches = $this->bunches + $counter;
175
176
        // reset the file resolver for making it ready parsing the files of the next subject
177 1
        $fileResolver->reset();
178
179
        // and and log a message that the subject has been processed
180 1
        $this->getSystemLogger()->debug(
181 1
            sprintf('Successfully processed subject "%s" with "%d" files)!', $subject->getId(), $counter)
182
        );
183 1
    }
184
}
185