ProvisionAlertListener   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 114
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 15
c 4
b 1
f 1
lcom 1
cbo 4
dl 12
loc 114
ccs 54
cts 54
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addAlerter() 0 4 1
A setCpuThresholds() 0 4 1
A setLoadAverageThresholds() 0 4 1
A setServer() 0 4 1
A setProcessStatusHandler() 0 4 1
C handle() 12 48 10

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 Innmind\ProvisionerBundle\EventListener;
4
5
use Innmind\ProvisionerBundle\Event\ProvisionAlertEvent;
6
use Innmind\ProvisionerBundle\Alert\AlerterInterface;
7
use Innmind\ProvisionerBundle\Alert\Alert;
8
use Innmind\ProvisionerBundle\Server\ServerInterface;
9
use Innmind\ProvisionerBundle\ProcessStatusHandler;
10
11
/**
12
 * Alerts when new servers need to be run or the current server
13
 * is no longer needed
14
 */
15
class ProvisionAlertListener
16
{
17
    protected $alerters = [];
18
    protected $cpuThreshold;
19
    protected $loadAverageThreshold;
20
    protected $server;
21
    protected $processStatus;
22
23
    /**
24
     * Add a new alerter
25
     *
26
     * @param AlerterInterface $alerter
27
     */
28 18
    public function addAlerter(AlerterInterface $alerter)
29
    {
30 18
        $this->alerters[] = $alerter;
31 18
    }
32
33
    /**
34
     * Set the CPU thresholds
35
     *
36
     * @param int $min
37
     * @param int $max
38
     */
39 18
    public function setCpuThresholds($min, $max)
40
    {
41 18
        $this->cpuThreshold = [(int) $min, (int) $max];
42 18
    }
43
44
    /**
45
     * Set the load average thresholds
46
     *
47
     * @param float $min
48
     * @param float $max
49
     */
50 18
    public function setLoadAverageThresholds($min, $max)
51
    {
52 18
        $this->loadAverageThreshold = [(float) $min, (float) $max];
53 18
    }
54
55
    /**
56
     * Set the server helper
57
     *
58
     * @param ServerInterface $server
59
     */
60 18
    public function setServer(ServerInterface $server)
61
    {
62 18
        $this->server = $server;
63 18
    }
64
65
    /**
66
     * Set the process status handler
67
     *
68
     * @param ProcessStatusHandler $handler
69
     */
70 18
    public function setProcessStatusHandler(ProcessStatusHandler $handler)
71
    {
72 18
        $this->processStatus = $handler;
73 18
    }
74
75
    /**
76
     * Check if an alert needs to be fired
77
     *
78
     * @param ProvisionAlertEvent $event
79
     */
80 18
    public function handle(ProvisionAlertEvent $event)
81
    {
82 18
        $leftOver = $event->getLeftOver();
83 18
        $cpuUsage = $this->server->getCpuUsage();
84 18
        $loadAverage = $this->server->getCurrentLoadAverage();
85
86 18
        $alert = new Alert();
87
        $alert
88 18
            ->setCommandName(
89 18
                $event->getCommandName()
90 18
            )
91 18
            ->setCommandInput(
92 18
                $event->getCommandInput()
93 18
            )
94 18
            ->setCpuUsage($cpuUsage)
95 18
            ->setLoadAverage($loadAverage)
96 18
            ->setRunningProcesses(
97 18
                $this->processStatus->getProcessCount(sprintf(
98 18
                    'console %s',
99 18
                    (string) $event->getCommandInput()
100 18
                ))
101 18
            )
102 18
            ->setLeftOver($leftOver);
103
104 18
        if ($leftOver === 0) {
105 View Code Duplication
            if (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106 9
                $cpuUsage <= $this->cpuThreshold[0] ||
107 6
                $loadAverage <= $this->loadAverageThreshold[0]
108 9
            ) {
109 6
                $alert->setUnderUsed();
110 6
            }
111 18
        } else if ($leftOver > 0) {
112 View Code Duplication
            if (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113 9
                $cpuUsage >= $this->cpuThreshold[1] ||
114 6
                $loadAverage >= $this->loadAverageThreshold[1]
115 9
            ) {
116 6
                $alert->setOverUsed();
117 6
            }
118 9
        }
119
120 18
        if (!$alert->isUnderUsed() && !$alert->isOverUsed()) {
121 6
            return;
122
        }
123
124 12
        foreach ($this->alerters as $alerter) {
125 12
            $alerter->alert($alert);
126 12
        }
127 12
    }
128
}
129