Completed
Push — 10.x ( c22aa1 )
by Tim
12:07
created

SubjectPlugin   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 86.05%

Importance

Changes 0
Metric Value
dl 0
loc 168
c 0
b 0
f 0
wmc 9
lcom 1
cbo 10
ccs 37
cts 43
cp 0.8605
rs 10

3 Methods

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