Completed
Pull Request — master (#22)
by
unknown
13:43
created

Manager::teardownTargets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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