StopExtension::init()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeStop;
4
5
use phmLabs\Components\Annovent\Dispatcher;
6
use phmLabs\Components\Annovent\Event\Event;
7
use PhmLabs\Components\Init\Init;
8
use whm\Smoke\Config\Configuration;
9
10
class StopExtension
11
{
12
    private $stopStrategies = array();
13
14
    /**
15
     * @param Configuration $_configuration
16
     * @param Dispatcher $_eventDispatcher
17
     * @throws \phmLabs\Components\Annovent\Exception
18
     */
19
    public function init(Configuration $_configuration, Dispatcher $_eventDispatcher)
20
    {
21
        if ($_configuration->hasSection('stop')) {
22
            $strategies = $_configuration->getSection('stop');
23
24
            foreach ($strategies as $name => $strategy) {
25
                $this->stopStrategies[$name] = Init::initialize($strategy);
26
                $_eventDispatcher->connectListener($this->stopStrategies[$name]);
27
            }
28
        }
29
    }
30
31
    /**
32
     * @Event("Scanner.Scan.isStopped")
33
     */
34
    public function isStopped(Event $event)
35
    {
36
        foreach ($this->stopStrategies as $strategy) {
37
            if ($strategy->isStopped()) {
38
                $event->setProcessed();
39
                return true;
40
            }
41
        }
42
43
        return false;
44
    }
45
46 View Code Duplication
    public function getStrategy($name)
47
    {
48
        if (array_key_exists($name, $this->stopStrategies)) {
49
            return $this->stopStrategies[$name];
50
        } else {
51
            throw new \RuntimeException("Strategy ('" . $name . "') not found. Available strategies are " . implode(', ', array_keys($this->stopStrategies)));
52
        }
53
    }
54
}
55