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

Configuration::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 3
crap 3
1
<?php
2
3
namespace HDNET\Importr\Processor;
4
5
use HDNET\Importr\Domain\Model\Strategy;
6
use HDNET\Importr\Domain\Repository\StrategyRepository;
7
use HDNET\Importr\Exception\ReinitializeException;
8
use HDNET\Importr\Service\ImportServiceInterface;
9
use HDNET\Importr\Service\ManagerInterface;
10
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
11
12
/**
13
 * Configuration
14
 */
15
class Configuration
16
{
17
    /**
18
     * @var Dispatcher
19
     */
20
    protected $signalSlotDispatcher;
21
22
    /**
23
     * @var StrategyRepository
24
     */
25
    protected $strategyRepository;
26
27
    /**
28
     * @var ImportServiceInterface
29
     */
30
    protected $importService;
31
32
    /**
33
     * Configuration constructor.
34
     * @param Dispatcher         $signalSlotDispatcher
35
     * @param StrategyRepository $strategyRepository
36
     */
37 6
    public function __construct(Dispatcher $signalSlotDispatcher, StrategyRepository $strategyRepository, ImportServiceInterface $importService)
38
    {
39 6
        $this->signalSlotDispatcher = $signalSlotDispatcher;
40 6
        $this->strategyRepository = $strategyRepository;
41 6
        $this->importService = $importService;
42 6
    }
43
44
    /**
45
     * @param array            $configuration
46
     * @param ManagerInterface $manager
47
     * @param mixed            $filter
48
     *
49
     * @throws ReinitializeException
50
     * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException
51
     * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException
52
     */
53 5
    public function process(array $configuration, ManagerInterface $manager, $filter = null)
54
    {
55 5
        if ($filter === null) {
56 3
            $this->processInner($configuration, $manager);
57 2
        } elseif ($this->canProcess($configuration, $filter)) {
58 1
            $this->processInner($configuration[$filter], $manager);
59
        }
60 4
    }
61
62
    /**
63
     * @param array  $configuration
64
     * @param string $name
65
     *
66
     * @return bool
67
     */
68 3
    public function canProcess(array $configuration, $name)
69
    {
70 3
        return isset($configuration[$name]) && is_array($configuration[$name]);
71
    }
72
73
    /**
74
     * @param array            $configuration
75
     * @param ManagerInterface $manager
76
     *
77
     * @throws ReinitializeException
78
     * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException
79
     * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException
80
     */
81 4
    protected function processInner(array $configuration, ManagerInterface $manager)
82
    {
83 4
        $this->emitSignal('preParseConfiguration', $configuration);
84
        try {
85 4
            $this->updateInterval($configuration, $manager)
86 4
                ->createImport($configuration)
87 4
                ->reinitializeScheduler($configuration);
88
89 3
            $this->emitSignal('postParseConfiguration', $configuration);
90 1
        } catch (ReinitializeException $exception) {
91 1
            $this->emitSignal('postParseConfiguration', $configuration);
92 1
            throw  $exception;
93
        }
94 3
    }
95
96
    /**
97
     * @param string $name
98
     * @param array  $configuration
99
     * @return $this
100
     * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException
101
     * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException
102
     */
103 4
    protected function emitSignal($name, array &$configuration)
104
    {
105 4
        $this->signalSlotDispatcher->dispatch(
106 4
            __CLASS__,
107
            $name,
108
            [
109 4
            $this,
110 4
            $configuration
111
            ]
112
        );
113 4
    }
114
115
    /**
116
     * @param array            $configuration
117
     * @param ManagerInterface $manager
118
     * @return $this
119
     */
120 4
    protected function updateInterval(array $configuration, ManagerInterface $manager)
121
    {
122 4
        if (isset($configuration['updateInterval'])) {
123 2
            $manager->setUpdateInterval((int)$configuration['updateInterval']);
124
        }
125
126 4
        return $this;
127
    }
128
129
    /**
130
     * @param array $configuration
131
     * @return $this
132
     * @throws ReinitializeException
133
     */
134 4
    protected function reinitializeScheduler(array $configuration)
135
    {
136 4
        if (isset($configuration['reinitializeScheduler'])) {
137 1
            throw new ReinitializeException();
138
        }
139
140 3
        return $this;
141
    }
142
143
    /**
144
     * @param array $configuration
145
     * @return $this
146
     */
147 4
    protected function createImport(array $configuration)
148
    {
149 4
        if (!isset($configuration['createImport']) && !is_array($configuration['createImport'])) {
150 3
            return $this;
151
        }
152
153 1
        foreach ($configuration['createImport'] as $create) {
154 1
            $strategy = $this->strategyRepository->findByUid((int)$create['importId']);
155
156 1
            if ($strategy instanceof Strategy) {
157 1
                $filepath = isset($create['filepath']) ? $create['filepath'] : '';
158 1
                $this->importService->addToQueue($filepath, $strategy, $create);
159
            }
160
        }
161
162 1
        return $this;
163
    }
164
}
165