Completed
Push — master ( 86437d...3559e6 )
by diego
08:58
created

Configuration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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