Completed
Push — 15.x ( 74665b )
by Tim
02:57
created

SubjectPlugin::process()   B

Complexity

Conditions 7
Paths 68

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.3112

Importance

Changes 0
Metric Value
dl 0
loc 60
c 0
b 0
f 0
ccs 22
cts 27
cp 0.8148
rs 7.9393
cc 7
nc 68
nop 0
crap 7.3112

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Configuration\SubjectConfigurationInterface;
27
use TechDivision\Import\Subjects\FileResolver\FileResolverFactoryInterface;
28
use TechDivision\Import\Exceptions\MissingFilesException;
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\Plugins\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\Plugins\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 1
    public function process()
99
    {
100
101
        try {
102
            // immediately add the PID to lock this import process
103 1
            $this->lock();
104
105
            // load the plugin's subjects
106 1
            $subjects = $this->getPluginConfiguration()->getSubjects();
107
108
            // initialize the array for the status
109 1
            $status = array();
110
111
            // initialize the status information for the subjects
112
            /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */
113 1
            foreach ($subjects as $subject) {
114
                $status[$subject->getPrefix()] = array();
115
            }
116
117
            // and update it in the registry
118 1
            $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 1
            foreach ($subjects as $subject) {
123
                $this->processSubject($subject);
124
            }
125
126
            // update the number of imported bunches
127 1
            $this->getRegistryProcessor()->mergeAttributesRecursive(
128 1
                RegistryKeys::STATUS,
129 1
                array(RegistryKeys::BUNCHES => $this->bunches)
130
            );
131
132
            // stop the application, because we didn't process any file
133 1
            if ($this->bunches === 0) {
134 1
                throw new MissingFilesException(
135 1
                    sprintf(
136 1
                        'Operation %s has been stopped because no import files can be found in directory %s',
137 1
                        $this->getConfiguration()->getOperationName(),
138 1
                        $this->getConfiguration()->getSourceDir()
139
                    )
140
                );
141
            }
142
143 1
        } catch (MissingOkFileException $mofe) {
144
            // stop the application if we can't find the mandatory OK file
145
            $this->getApplication()->stop($mofe->getMessage());
146 1
        } catch (MissingFilesException $mfe) {
147
            // stop the application if can't find at least one file to process
148 1
            $this->getApplication()->stop($mfe->getMessage());
149
        } catch (\Exception $e) {
150
            // re-throw the exception
151
            throw $e;
152 1
        } finally {
153
            // finally, if a PID has been set, remove it
154
            // from the PID file to unlock the importer
155 1
            $this->unlock();
156
        }
157 1
    }
158
159
    /**
160
     * Process the subject with the passed name/identifier.
161
     *
162
     * We create a new, fresh and separate subject for EVERY file here, because this would be
163
     * the starting point to parallelize the import process in a multithreaded/multiprocessed
164
     * environment.
165
     *
166
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The subject configuration
167
     *
168
     * @return void
169
     */
170
    protected function processSubject(SubjectConfigurationInterface $subject)
171
    {
172
173
        // initialize the bunch number
174
        $bunches = 0;
175
176
        // load the file resolver for the subject with the passed configuration
177
        $fileResolver = $this->fileResolverFactory->createFileResolver($subject);
178
179
        // load the files
180
        $files = $fileResolver->loadFiles($serial = $this->getSerial());
181
182
        // iterate through all CSV files and process the subjects
183
        foreach ($files as $filename) {
184
            // initialize the subject and import the bunch
185
            $this->subjectExecutor->execute($subject, $fileResolver->getMatches(), $serial, $filename);
186
            // raise the number of the imported bunches
187
            $bunches++;
188
        }
189
190
        // raise the bunch number by the imported bunches
191
        $this->bunches = $this->bunches + $bunches;
192
193
        // reset the file resolver for making it ready parsing the files of the next subject
194
        $fileResolver->reset();
195
196
        // and and log a message that the subject has been processed
197
        $this->getSystemLogger()->debug(
198
            sprintf('Successfully processed subject %s with %d bunches)!', $subject->getId(), $bunches)
199
        );
200
    }
201
}
202