Completed
Push — master ( 097c86...e0d09c )
by diego
05:32
created

Manager::emitSignal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 2
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->emitSignal('preImport', $import);
115
116
        $resources = $this->initializeResourcesByImport($import);
117
        $targets = $this->initializeTargets($import);
118
        $strategyConfiguration = $import->getStrategy()
119
            ->getConfiguration();
120
121
        foreach ($resources as $resource) {
122
            if ($this->resource->process($import, $targets, $strategyConfiguration, $resource, $this)) {
123
                break;
124
            }
125
        }
126
127
        $this->emitSignal('postImport', $import);
128
    }
129
130
    /**
131
     * @param \HDNET\Importr\Domain\Model\Import $import
132
     *
133
     * @return array
134
     */
135
    protected function initializeResourcesByImport(Import $import)
136
    {
137
        return $this->initializeResources($import->getStrategy(), $import->getFilepath());
138
    }
139
140
    /**
141
     * @param \HDNET\Importr\Domain\Model\Strategy $strategy
142
     * @param string                               $filepath
143
     *
144
     * @return array
145
     */
146 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...
147
    {
148 1
        $resources = [];
149 1
        $resourceConfiguration = $strategy->getResources();
150 1
        foreach ($resourceConfiguration as $resource => $configuration) {
151 1
            $object = $this->objectManager->get($resource);
152 1
            $object->start($strategy, $filepath);
153 1
            $object->setConfiguration($configuration);
154 1
            $resources[$resource] = $object;
155
        }
156 1
        return $resources;
157
    }
158
159
    /**
160
     * @param \HDNET\Importr\Domain\Model\Import $import
161
     *
162
     * @return array
163
     */
164 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...
165
    {
166
        $targets = [];
167
        $targetConfiguration = $import->getStrategy()
168
            ->getTargets();
169
        foreach ($targetConfiguration as $target => $configuration) {
170
            $object = $this->objectManager->get($target);
171
            $object->setConfiguration($configuration);
172
            $object->getConfiguration();
173
            $object->start($import->getStrategy());
174
            $targets[$target] = $object;
175
        }
176
        return $targets;
177
    }
178
179
    /**
180
     * @param int $interval
181
     */
182 1
    public function setUpdateInterval($interval)
183
    {
184 1
        $this->updateInterval = $interval;
185 1
    }
186
187
    /**
188
     * @return int
189
     */
190 1
    public function getUpdateInterval()
191
    {
192 1
        return $this->updateInterval;
193
    }
194
195
    /**
196
     * @param string $name
197
     * @param Import $import
198
     */
199
    protected function emitSignal($name, Import $import)
200
    {
201
        $this->signalSlotDispatcher->dispatch(
202
            __CLASS__,
203
            $name,
204
            [$this, $import]
205
        );
206
    }
207
}
208