ProcessStatusHandler   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 18.69 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 1
dl 20
loc 107
ccs 35
cts 37
cp 0.9459
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setServer() 0 4 1
A setUsePrecision() 0 4 1
A getProcessCount() 10 10 2
A getProcessUsage() 10 10 2
B computeProcessInformations() 0 23 4
A getKey() 0 4 1

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;
4
5
use Innmind\ProvisionerBundle\Server\ServerInterface;
6
7
/**
8
 * Holds informations about a process througthout a symfony runtime
9
 */
10
class ProcessStatusHandler
11
{
12
    const COMPUTE_ITERATIONS = 3;
13
    const ITERATIONS_SLEEP = 2;
14
15
    protected $informations = [];
16
    protected $server;
17
    protected $usePrecision = true;
18
19
    /**
20
     * Set the server helper
21
     *
22
     * @param ServerInterface $server
23
     */
24 39
    public function setServer(ServerInterface $server)
25
    {
26 39
        $this->server = $server;
27 39
    }
28
29
    /**
30
     * Whether to run compute multiple times cpu usage
31
     * to get a better approximation or not
32
     *
33
     * @param bool $use
34
     */
35 39
    public function setUsePrecision($use)
36
    {
37 39
        $this->usePrecision = (bool) $use;
38 39
    }
39
40
    /**
41
     * Return the number of process running for the givent command
42
     *
43
     * @param string $command
44
     *
45
     * @return int
46
     */
47 27 View Code Duplication
    public function getProcessCount($command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
48
    {
49 27
        $hash = $this->getKey($command);
50
51 27
        if (!isset($this->informations[$hash])) {
52 24
            $this->computeProcessInformations($command);
53 24
        }
54
55 27
        return $this->informations[$hash]['number'];
56
    }
57
58
    /**
59
     * Return the average CPU usage for the given command
60
     *
61
     * @param string $command
62
     *
63
     * @return float
64
     */
65 6 View Code Duplication
    public function getProcessUsage($command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
66
    {
67 6
        $hash = $this->getKey($command);
68
69 6
        if (!isset($this->informations[$hash])) {
70 6
            $this->computeProcessInformations($command);
71 6
        }
72
73 6
        return $this->informations[$hash]['usage'];
74
    }
75
76
    /**
77
     * Compute the process usage/number for the given command
78
     *
79
     * @param string $command
80
     */
81 30
    protected function computeProcessInformations($command)
82
    {
83 30
        $averageProcessUsage = 0;
84 30
        $averageProcessNumber = 0;
85 30
        $iterations = $this->usePrecision ? self::COMPUTE_ITERATIONS : 1;
86
87 30
        for ($i = 0; $i < $iterations; $i++) {
88 30
            $averageProcessUsage += $this->server->getProcessUsage($command);
89 30
            $averageProcessNumber += $this->server->getProcessCount($command);
90
91 30
            if ($this->usePrecision === true) {
92
                sleep(self::ITERATIONS_SLEEP);
93
            }
94 30
        }
95
96 30
        $averageProcessUsage = $averageProcessUsage / $iterations;
97 30
        $averageProcessNumber = floor($averageProcessNumber / $iterations);
98
99 30
        $this->informations[$this->getKey($command)] = [
100 30
            'usage' => (float) $averageProcessUsage,
101
            'number' => (int) $averageProcessNumber
102 30
        ];
103 30
    }
104
105
    /**
106
     * Return a unique key for the given command
107
     *
108
     * @param string $command
109
     *
110
     * @return string
111
     */
112 30
    protected function getKey($command)
113
    {
114 30
        return md5($command);
115
    }
116
}
117