Completed
Push — master ( af13d1...2fe979 )
by diego
03:25
created

Manager::runImport()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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