Completed
Push — master ( 543fe1...7ff5ba )
by diego
14:03
created

Manager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 4
cts 4
cp 1
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 6
crap 1
1
<?php
2
3
declare(strict_types=1);
4
namespace HDNET\Importr\Service;
5
6
use HDNET\Importr\Domain\Model\Import;
7
use HDNET\Importr\Domain\Model\Strategy;
8
use HDNET\Importr\Domain\Repository\ImportRepository;
9
use HDNET\Importr\Exception\ReinitializeException;
10
use HDNET\Importr\Processor\Configuration;
11
use HDNET\Importr\Processor\Resource;
12
use TYPO3\CMS\Extbase\Object\ObjectManager;
13
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
14
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
15
16
/**
17
 * Service Manager
18
 *
19
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
20
 * @author  Tim Lochmüller <[email protected]>
21
 */
22
class Manager implements ManagerInterface
23
{
24
25
    /**
26
     * @var \HDNET\Importr\Domain\Repository\ImportRepository
27
     */
28
    protected $importRepository;
29
30
    /**
31
     * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
32
     */
33
    protected $signalSlotDispatcher;
34
35
    /**
36
     * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
37
     */
38
    protected $persistenceManager;
39
40
    /**
41
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
42
     */
43
    protected $objectManager;
44
45
    /**
46
     * @var \HDNET\Importr\Processor\Configuration
47
     */
48
    protected $configuration;
49
50
    /**
51
     * @var \HDNET\Importr\Processor\Resource
52
     */
53
    protected $resource;
54
55
    public function __construct(
56
        ImportRepository $importRepository,
57
        Dispatcher $signalSlotDispatcher,
58
        PersistenceManager $persistenceManager,
59
        ObjectManager $objectManager,
60
        Configuration $configuration,
61
        Resource $resource
62
    ) {
63 1
        $this->importRepository = $importRepository;
64
        $this->signalSlotDispatcher = $signalSlotDispatcher;
65
        $this->persistenceManager = $persistenceManager;
66 1
        $this->objectManager = $objectManager;
67 1
        $this->configuration = $configuration;
68 1
        $this->resource = $resource;
69
    }
70
71
    /**
72
     * Update Interval
73 1
     *
74
     * @var int
75
     */
76
    protected $updateInterval = 1;
77
78
    /**
79
     * run the Importr
80
     */
81
    public function runImports()
82
    {
83 1
        try {
84
            $imports = $this->importRepository->findWorkQueue();
85 1
            foreach ($imports as $import) {
86 1
                $this->runImport($import);
87 1
            }
88
        } catch (ReinitializeException $exc) {
89
            $this->runImports();
90
        }
91
    }
92 1
93
    /**
94 1
     * Get the preview
95
     *
96 1
     * @param string                               $filepath
97 1
     * @param \HDNET\Importr\Domain\Model\Strategy $strategy
98 1
     *
99
     * @return array
100
     */
101 1
    public function getPreview(Strategy $strategy, $filepath)
102
    {
103
        $data = [];
104 1
        $resources = $this->initializeResources($strategy, $filepath);
105
        foreach ($resources as $resource) {
106
            /**
107
 * @var \HDNET\Importr\Service\Resources\ResourceInterface $resource
108
*/
109
            // Resourcen Object anhand der Datei auswählen
110
            if (\preg_match($resource->getFilepathExpression(), $filepath)) {
111
                // Resource "benutzen"
112
                $resource->parseResource();
113
                // Durchlauf starten
114
                for ($pointer = 0; $pointer <= 20; $pointer++) {
115
                    if ($resource->getEntry($pointer)) {
116
                        $data[] = $resource->getEntry($pointer);
117
                    }
118
                }
119
                break;
120
            }
121
        }
122
        return $data;
123
    }
124
125
    /**
126
     * Magic Runner
127
     *
128
     * @param \HDNET\Importr\Domain\Model\Import $import
129
     */
130
    protected function runImport(Import $import)
131
    {
132
        $this->emitSignal('preImport', $import);
133
134
        $resources = $this->initializeResourcesByImport($import);
135
        $targets = $this->initializeTargets($import);
136
        $strategyConfiguration = $import->getStrategy()
137
            ->getConfiguration();
138
139
        foreach ($resources as $resource) {
140
            if ($this->resource->process($import, $targets, $strategyConfiguration, $resource, $this)) {
141
                break;
142
            }
143
        }
144
145
        $this->emitSignal('postImport', $import);
146 1
    }
147
148 1
    /**
149 1
     * @param \HDNET\Importr\Domain\Model\Import $import
150 1
     *
151 1
     * @return array
152 1
     */
153 1
    protected function initializeResourcesByImport(Import $import)
154 1
    {
155
        return $this->initializeResources($import->getStrategy(), $import->getFilepath());
156 1
    }
157
158
    /**
159
     * @param \HDNET\Importr\Domain\Model\Strategy $strategy
160
     * @param string                               $filepath
161
     *
162
     * @return array
163
     */
164 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...
165
    {
166
        $resources = [];
167
        $resourceConfiguration = $strategy->getResources();
168
        foreach ($resourceConfiguration as $resource => $configuration) {
169
            $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...
170
            $object->start($strategy, $filepath);
171
            $object->setConfiguration($configuration);
172
            $resources[$resource] = $object;
173
        }
174
        return $resources;
175
    }
176
177
    /**
178
     * @param \HDNET\Importr\Domain\Model\Import $import
179
     *
180
     * @return array
181
     */
182 1 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...
183
    {
184 1
        $targets = [];
185 1
        $targetConfiguration = $import->getStrategy()
186
            ->getTargets();
187
        foreach ($targetConfiguration as $target => $configuration) {
188
            $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...
189
            $object->setConfiguration($configuration);
190 1
            $object->getConfiguration();
191
            $object->start($import->getStrategy());
192 1
            $targets[$target] = $object;
193
        }
194
        return $targets;
195
    }
196
197
    /**
198
     * @param int $interval
199
     */
200
    public function setUpdateInterval($interval)
201
    {
202
        $this->updateInterval = $interval;
203
    }
204
205
    /**
206
     * @return int
207
     */
208
    public function getUpdateInterval()
209
    {
210
        return $this->updateInterval;
211
    }
212
213
    /**
214
     * @param string $name
215
     * @param Import $import
216
     */
217
    protected function emitSignal($name, Import $import)
218
    {
219
        $this->signalSlotDispatcher->dispatch(
220
            __CLASS__,
221
            $name,
222
            [$this, $import]
223
        );
224
    }
225
}
226