MemoryConsumptionChecker   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 78.26%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 54
ccs 18
cts 23
cp 0.7826
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isRamAlmostOverloaded() 0 7 1
A __construct() 0 3 1
A convertHumanUnitToNumerical() 0 19 5
1
<?php
2
3
namespace OldSound\RabbitMqBundle\MemoryChecker;
4
5
/**
6
 * Help handling memory limits .
7
 *
8
 * @author Jonas Haouzi <[email protected]>
9
 */
10
class MemoryConsumptionChecker
11
{
12
    /** @var NativeMemoryUsageProvider */
13
    private $memoryUsageProvider;
14
15
    /**
16
     * MemoryManager constructor.
17
     *
18
     * @param NativeMemoryUsageProvider $memoryUsageProvider
19
     */
20 4
    public function __construct(NativeMemoryUsageProvider $memoryUsageProvider)
21
    {
22 4
        $this->memoryUsageProvider = $memoryUsageProvider;
23 4
    }
24
25
    /**
26
     * @param int|string $allowedConsumptionUntil
27
     * @param int|string $maxConsumptionAllowed
28
     *
29
     * @return bool
30
     */
31 4
    public function isRamAlmostOverloaded($maxConsumptionAllowed, $allowedConsumptionUntil = 0)
32
    {
33 4
        $allowedConsumptionUntil = $this->convertHumanUnitToNumerical($allowedConsumptionUntil);
34 4
        $maxConsumptionAllowed = $this->convertHumanUnitToNumerical($maxConsumptionAllowed);
35 4
        $currentUsage = $this->convertHumanUnitToNumerical($this->memoryUsageProvider->getMemoryUsage());
36
37 4
        return $currentUsage > ($maxConsumptionAllowed - $allowedConsumptionUntil);
38
    }
39
40
    /**
41
     * @param int|string $humanUnit
42
     *
43
     * @return int
44
     */
45 4
    private function convertHumanUnitToNumerical($humanUnit)
46
    {
47 4
        $numerical = $humanUnit;
48 4
        if (!is_numeric($humanUnit)) {
49 4
            $numerical = (int) substr($numerical, 0, -1);
50 4
            switch (substr($humanUnit, -1)) {
51 4
                case 'G':
52
                    $numerical *= pow(1024, 3);
53
                    break;
54 4
                case 'M':
55 4
                    $numerical *= pow(1024, 2);
56 4
                    break;
57
                case 'K':
58
                    $numerical *= 1024;
59
                    break;
60
            }
61
        }
62
63 4
        return (int)$numerical;
64
    }
65
}
66