Completed
Branch master (1f9106)
by Nils
02:44
created

StopExtension::isStopped()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
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
    public function init(Configuration $_configuration, Dispatcher $_eventDispatcher)
15
    {
16
        if ($_configuration->hasSection('stop')) {
17
            $strategies = $_configuration->getSection('stop');
18
19
            foreach ($strategies as $name => $strategy) {
20
                $this->stopStrategies[$name] = Init::initialize($strategy);
21
                $_eventDispatcher->connectListener($this->stopStrategies[$name]);
22
            }
23
        }
24
    }
25
26
    /**
27
     * @Event("Scanner.Scan.isStopped")
28
     */
29
    public function isStopped(Event $event)
30
    {
31
        foreach ($this->stopStrategies as $strategy) {
32
            if ($strategy->isStopped()) {
33
                $event->setProcessed();
34
35
                return true;
36
            }
37
        }
38
39
        return false;
40
    }
41
42 View Code Duplication
    public function getStrategy($name)
43
    {
44
        if (array_key_exists($name, $this->stopStrategies)) {
45
            return $this->stopStrategies[$name];
46
        } else {
47
            throw new \RuntimeException("Strategy ('" . $name . "') not found. Available strategies are " . implode(', ', array_keys($this->stopStrategies)));
48
        }
49
    }
50
}
51