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

StopExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 19.51 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 8
c 3
b 1
f 0
lcom 1
cbo 4
dl 8
loc 41
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 11 3
A getStrategy() 8 8 2
A isStopped() 0 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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