Completed
Push — master ( d0a785...86437d )
by diego
10:47
created

Manager::teardownTargets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 5
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HDNET\Importr\Service;
6
7
use HDNET\Importr\Domain\Model\Import;
8
use HDNET\Importr\Domain\Model\Strategy;
9
use HDNET\Importr\Domain\Repository\ImportRepository;
10
use HDNET\Importr\Exception\ReinitializeException;
11
use HDNET\Importr\Processor\Configuration;
12
use HDNET\Importr\Processor\Resource;
13
use TYPO3\CMS\Extbase\Object\ObjectManager;
14
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
15
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
16
17
/**
18
 * Service Manager
19
 *
20
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
21
 * @author  Tim Lochmüller <[email protected]>
22
 */
23
class Manager implements ManagerInterface
24
{
25
26
    /**
27
     * @var \HDNET\Importr\Domain\Repository\ImportRepository
28
     */
29
    protected $importRepository;
30
31
    /**
32
     * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
33
     */
34
    protected $signalSlotDispatcher;
35
36
    /**
37
     * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
38
     */
39
    protected $persistenceManager;
40
41
    /**
42
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
43
     */
44
    protected $objectManager;
45
46
    /**
47
     * @var \HDNET\Importr\Processor\Configuration
48
     */
49
    protected $configuration;
50
51
    /**
52
     * @var \HDNET\Importr\Processor\Resource
53
     */
54
    protected $resource;
55
56
    public function __construct(
57
        ImportRepository $importRepository,
58
        Dispatcher $signalSlotDispatcher,
59
        PersistenceManager $persistenceManager,
60
        ObjectManager $objectManager,
61
        Configuration $configuration,
62
        Resource $resource
63 1
    ) {
64
        $this->importRepository = $importRepository;
65
        $this->signalSlotDispatcher = $signalSlotDispatcher;
66 1
        $this->persistenceManager = $persistenceManager;
67 1
        $this->objectManager = $objectManager;
68 1
        $this->configuration = $configuration;
69
        $this->resource = $resource;
70
    }
71
72
    /**
73 1
     * Update Interval
74
     *
75
     * @var int
76
     */
77
    protected $updateInterval = 1;
78
79
    /**
80
     * run the Importr
81
     */
82
    public function runImports()
83 1
    {
84
        try {
85 1
            $imports = $this->importRepository->findWorkQueue();
86 1
            foreach ($imports as $import) {
87 1
                $this->runImport($import);
88
            }
89
        } catch (ReinitializeException $exc) {
90
            $this->runImports();
91
        }
92 1
    }
93
94 1
    /**
95
     * Get the preview
96 1
     *
97 1
     * @param string                               $filepath
98 1
     * @param \HDNET\Importr\Domain\Model\Strategy $strategy
99
     *
100
     * @return array
101 1
     */
102
    public function getPreview(Strategy $strategy, $filepath)
103
    {
104 1
        $data = [];
105
        $resources = $this->initializeResources($strategy, $filepath);
106
        foreach ($resources as $resource) {
107
            /** @var \HDNET\Importr\Service\Resources\ResourceInterface $resource */
108
            // Resourcen Object anhand der Datei auswählen
109
            if (\preg_match($resource->getFilepathExpression(), $filepath)) {
110
                // Resource "benutzen"
111
                $resource->parseResource();
112
                // Durchlauf starten
113
                for ($pointer = 0; $pointer <= 20; $pointer++) {
114
                    if ($resource->getEntry($pointer)) {
115
                        $data[] = $resource->getEntry($pointer);
116
                    }
117
                }
118
                break;
119
            }
120
        }
121
        return $data;
122
    }
123
124
    /**
125
     * Magic Runner
126
     *
127
     * @param \HDNET\Importr\Domain\Model\Import $import
128
     */
129
    protected function runImport(Import $import)
130
    {
131
        $this->emitSignal('preImport', $import);
132
133
        $resources = $this->initializeResourcesByImport($import);
134
        $targets = $this->initializeTargets($import);
135
        $strategyConfiguration = $import->getStrategy()
136
            ->getConfiguration();
137
138
        foreach ($resources as $resource) {
139
            if ($this->resource->process($import, $targets, $strategyConfiguration, $resource, $this)) {
140
                break;
141
            }
142
        }
143
144
        $this->teardownTargets($import);
145
146 1
        $this->emitSignal('postImport', $import);
147
    }
148 1
149 1
    /**
150 1
     * @param \HDNET\Importr\Domain\Model\Import $import
151 1
     *
152 1
     * @return array
153 1
     */
154 1
    protected function initializeResourcesByImport(Import $import)
155
    {
156 1
        return $this->initializeResources($import->getStrategy(), $import->getFilepath());
157
    }
158
159
    /**
160
     * @param \HDNET\Importr\Domain\Model\Strategy $strategy
161
     * @param string                               $filepath
162
     *
163
     * @return array
164
     */
165 View Code Duplication
    protected function initializeResources(Strategy $strategy, $filepath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
        $resources = [];
168
        $resourceConfiguration = $strategy->getResources();
169
        foreach ($resourceConfiguration as $resource => $configuration) {
170
            $object = $this->objectManager->get($resource);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Extbase\Object\ObjectManager::get() has been deprecated with message: since TYPO3 10.4, will be removed in version 12.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
171
            $object->start($strategy, $filepath);
172
            $object->setConfiguration($configuration);
173
            $resources[$resource] = $object;
174
        }
175
        return $resources;
176
    }
177
178
    /**
179
     * @param \HDNET\Importr\Domain\Model\Import $import
180
     *
181
     * @return array
182 1
     */
183 View Code Duplication
    protected function initializeTargets(Import $import)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184 1
    {
185 1
        $targets = [];
186
        $targetConfiguration = $import->getStrategy()
187
            ->getTargets();
188
        foreach ($targetConfiguration as $target => $configuration) {
189
            $object = $this->objectManager->get($target);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Extbase\Object\ObjectManager::get() has been deprecated with message: since TYPO3 10.4, will be removed in version 12.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
190 1
            $object->setConfiguration($configuration);
191
            $object->getConfiguration();
192 1
            $object->start($import->getStrategy());
193
            $targets[$target] = $object;
194
        }
195
        return $targets;
196
    }
197
198
    /**
199
     * @param \HDNET\Importr\Domain\Model\Import $import
200
     */
201
    protected function teardownTargets(Import $import)
202
    {
203
        $targetConfiguration = $import->getStrategy()
204
            ->getTargets();
205
        foreach ($targetConfiguration as $target => $configuration) {
206
            $object = $this->objectManager->get($target);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Extbase\Object\ObjectManager::get() has been deprecated with message: since TYPO3 10.4, will be removed in version 12.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
207
            $object->setConfiguration($configuration);
208
            $object->getConfiguration();
209
            $object->end($import->getStrategy());
210
        }
211
    }
212
213
    /**
214
     * @param int $interval
215
     */
216
    public function setUpdateInterval($interval)
217
    {
218
        $this->updateInterval = $interval;
219
    }
220
221
    /**
222
     * @return int
223
     */
224
    public function getUpdateInterval()
225
    {
226
        return $this->updateInterval;
227
    }
228
229
    /**
230
     * @param string $name
231
     * @param Import $import
232
     */
233
    protected function emitSignal($name, Import $import)
234
    {
235
        $this->signalSlotDispatcher->dispatch(
236
            __CLASS__,
237
            $name,
238
            [$this, $import]
239
        );
240
    }
241
}
242