1 | <?php |
||
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( |
|
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) |
|
204 | } |
||
205 |