Completed
Push — master ( 015490...3fff3d )
by diego
06:23 queued 04:33
created

Manager::getUpdateInterval()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 2
    public function __construct(
57
        ImportRepository $importRepository,
58
        Dispatcher $signalSlotDispatcher,
59
        PersistenceManager $persistenceManager,
60
        ObjectManager $objectManager,
61
        Configuration $configuration,
62
        Resource $resource
63
    ) {
64 2
        $this->importRepository = $importRepository;
65 2
        $this->signalSlotDispatcher = $signalSlotDispatcher;
66 2
        $this->persistenceManager = $persistenceManager;
67 2
        $this->objectManager = $objectManager;
68 2
        $this->configuration = $configuration;
69 2
        $this->resource = $resource;
70 2
    }
71
72
    /**
73
     * Update Interval
74
     *
75
     * @var int
76
     */
77
    protected $updateInterval = 1;
78
79
    /**
80
     * run the Importr
81
     */
82 1
    public function runImports()
83
    {
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
    /**
95
     * Get the preview
96
     *
97
     * @param string                               $filepath
98
     * @param \HDNET\Importr\Domain\Model\Strategy $strategy
99
     *
100
     * @return array
101
     */
102 1
    public function getPreview(Strategy $strategy, $filepath)
103
    {
104 1
        $data = [];
105 1
        $resources = $this->initializeResources($strategy, $filepath);
106 1
        foreach ($resources as $resource) {
107
            /** @var \HDNET\Importr\Service\Resources\ResourceInterface $resource */
108
            // Resourcen Object anhand der Datei auswählen
109 1
            if (\preg_match($resource->getFilepathExpression(), $filepath)) {
110
                // Resource "benutzen"
111 1
                $resource->parseResource();
112
                // Durchlauf starten
113 1
                for ($pointer = 0; $pointer <= 20; $pointer++) {
114 1
                    if ($resource->getEntry($pointer)) {
115 1
                        $data[] = $resource->getEntry($pointer);
116
                    }
117
                }
118 1
                break;
119
            }
120
        }
121 1
        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
        $this->emitSignal('postImport', $import);
147
    }
148
149
    /**
150
     * @param \HDNET\Importr\Domain\Model\Import $import
151
     *
152
     * @return array
153
     */
154
    protected function initializeResourcesByImport(Import $import)
155
    {
156
        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 1
    protected function initializeResources(Strategy $strategy, $filepath)
166
    {
167 1
        $resources = [];
168 1
        $resourceConfiguration = $strategy->getResources();
169 1
        foreach ($resourceConfiguration as $resource => $configuration) {
170 1
            $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 1
            $object->start($strategy, $filepath);
172 1
            $object->setConfiguration($configuration);
173 1
            $resources[$resource] = $object;
174
        }
175 1
        return $resources;
176
    }
177
178
    /**
179
     * @param \HDNET\Importr\Domain\Model\Import $import
180
     *
181
     * @return array
182
     */
183
    protected function initializeTargets(Import $import)
184
    {
185
        $targetConfiguration = $import->getStrategy()
186
            ->getTargets();
187
        return $this->addTargets($targetConfiguration, $import);
188
    }
189
190
    protected function addTargets(array $targetConfiguration, Import $import, array $targets = []): array
191
    {
192
        foreach ($targetConfiguration as $target => $configuration) {
193
            if (\is_numeric($target)) {
194
                $targets = $this->addTargets($configuration, $import, $targets);
195
                continue;
196
            }
197
            $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...
198
            $object->setConfiguration($configuration);
199
            $object->getConfiguration();
200
            $object->start($import->getStrategy());
201
            $targets[] = $object;
202
        }
203
        return $targets;
204
    }
205
206
    /**
207
     * @param \HDNET\Importr\Domain\Model\Import $import
208
     */
209
    protected function teardownTargets(Import $import)
210
    {
211
        $targetConfiguration = $import->getStrategy()
212
            ->getTargets();
213
        $this->endTargets($targetConfiguration, $import);
214
    }
215
216
    protected function endTargets(array $targetConfiguration, Import $import): void
217
    {
218
        foreach ($targetConfiguration as $target => $configuration) {
219
            if (\is_numeric($target)) {
220
                $this->endTargets($configuration, $import);
221
                continue;
222
            }
223
            $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...
224
            $object->setConfiguration($configuration);
225
            $object->getConfiguration();
226
            $object->end($import->getStrategy());
227
        }
228
    }
229
230
    /**
231
     * @param int $interval
232
     */
233 1
    public function setUpdateInterval($interval)
234
    {
235 1
        $this->updateInterval = $interval;
236 1
    }
237
238
    /**
239
     * @return int
240
     */
241 1
    public function getUpdateInterval()
242
    {
243 1
        return $this->updateInterval;
244
    }
245
246
    /**
247
     * @param string $name
248
     * @param Import $import
249
     */
250
    protected function emitSignal($name, Import $import)
251
    {
252
        $this->signalSlotDispatcher->dispatch(
253
            __CLASS__,
254
            $name,
255
            [$this, $import]
256
        );
257
    }
258
}
259