Completed
Push — master ( 015490...3fff3d )
by diego
06:23 queued 04:33
created

Configuration   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 5
dl 0
loc 149
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A emitSignal() 0 8 1
A __construct() 0 6 1
A process() 0 8 3
A canProcess() 0 4 2
A processInner() 0 14 2
A updateInterval() 0 8 2
A reinitializeScheduler() 0 8 2
A createImport() 0 17 6
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 6
    public function __construct(Dispatcher $dispatcher, StrategyRepository $strategyRepository, ImportServiceInterface $importService)
42
    {
43 6
        $this->dispatcher = $dispatcher;
44 6
        $this->strategyRepository = $strategyRepository;
45 6
        $this->importService = $importService;
46 6
    }
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 5
    public function process(array $configuration, ManagerInterface $manager, $filter = null)
58
    {
59 5
        if ($filter === null) {
60 3
            $this->processInner($configuration, $manager);
61 2
        } elseif ($this->canProcess($configuration, $filter)) {
62 1
            $this->processInner($configuration[$filter], $manager);
63
        }
64 4
    }
65
66
    /**
67
     * @param array  $configuration
68
     * @param string $name
69
     *
70
     * @return bool
71
     */
72 3
    public function canProcess(array $configuration, $name)
73
    {
74 3
        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 4
    protected function processInner(array $configuration, ManagerInterface $manager)
86
    {
87 4
        $this->emitSignal('preParseConfiguration', $configuration);
88
        try {
89 4
            $this->updateInterval($configuration, $manager)
90 4
                ->createImport($configuration)
91 4
                ->reinitializeScheduler($configuration);
92
93 3
            $this->emitSignal('postParseConfiguration', $configuration);
94 1
        } catch (ReinitializeException $exception) {
95 1
            $this->emitSignal('postParseConfiguration', $configuration);
96 1
            throw  $exception;
97
        }
98 3
    }
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 4
    protected function emitSignal($name, array &$configuration)
108
    {
109 4
        $this->dispatcher->dispatch(
110 4
            __CLASS__,
111
            $name,
112 4
            [$this, $configuration]
113
        );
114 4
    }
115
116
    /**
117
     * @param array            $configuration
118
     * @param ManagerInterface $manager
119
     * @return $this
120
     */
121 4
    protected function updateInterval(array $configuration, ManagerInterface $manager)
122
    {
123 4
        if (isset($configuration['updateInterval'])) {
124 2
            $manager->setUpdateInterval((int)$configuration['updateInterval']);
125
        }
126
127 4
        return $this;
128
    }
129
130
    /**
131
     * @param array $configuration
132
     * @return $this
133
     * @throws ReinitializeException
134
     */
135 4
    protected function reinitializeScheduler(array $configuration)
136
    {
137 4
        if (isset($configuration['reinitializeScheduler'])) {
138 1
            throw new ReinitializeException();
139
        }
140
141 3
        return $this;
142
    }
143
144
    /**
145
     * @param array $configuration
146
     * @return $this
147
     */
148 4
    protected function createImport(array $configuration)
149
    {
150 4
        if (!isset($configuration['createImport']) || !\is_array($configuration['createImport'])) {
151 3
            return $this;
152
        }
153
154 1
        foreach ($configuration['createImport'] as $create) {
155 1
            $strategy = $this->strategyRepository->findByUid((int)$create['importId']);
156
157 1
            if ($strategy instanceof Strategy) {
158 1
                $filepath = isset($create['filepath']) ? $create['filepath'] : '';
159 1
                $this->importService->addToQueue($filepath, $strategy, $create);
160
            }
161
        }
162
163 1
        return $this;
164
    }
165
}
166