ProvisionAlertListener::handle()   C
last analyzed

Complexity

Conditions 10
Paths 15

Size

Total Lines 48
Code Lines 31

Duplication

Lines 12
Ratio 25 %

Code Coverage

Tests 39
CRAP Score 10

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 12
loc 48
ccs 39
cts 39
cp 1
rs 5.3455
cc 10
eloc 31
nc 15
nop 1
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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