GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MemoryLimit::onAfterRun()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace dmank\gearman\event\subscriber;
4
5
use dmank\gearman\event\WorkerEvent;
6
use Psr\Log\LoggerInterface;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
9
class MemoryLimit implements EventSubscriberInterface
10
{
11
    private $maxMemoryLimit;
12
13
    /**
14
     * @var LoggerInterface
15
     */
16
    private $logger;
17
18 2
    public function __construct($maxMemoryLimit = -1, LoggerInterface $logger = null)
19
    {
20 2
        $this->maxMemoryLimit = $maxMemoryLimit;
21 2
        $this->logger = $logger;
22 2
    }
23
24
    public static function getSubscribedEvents()
25
    {
26
        return array(
27
            WorkerEvent::EVENT_AFTER_RUN => array('onAfterRun'),
28
            WorkerEvent::EVENT_BEFORE_RUN => array('onBeforeRun')
29
        );
30
    }
31
32
    public function onBeforeRun(WorkerEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        if (!is_null($this->logger) && $this->maxMemoryLimit != '-1') {
35
            $this->logger->notice(
36
                sprintf(
37
                    'i will stop the worker when the memory limit of %s is exceeded.',
38
                    $this->maxMemoryLimit
39
                )
40
            );
41
        }
42
    }
43
44 2
    public function onAfterRun(WorkerEvent $event)
45
    {
46 2
        if ($this->memoryLimitAlmostExceeded()) {
47 1
            $this->informLogger();
48
49 1
            $event->getWorkerInstance()->destroyWorker();
50 1
        }
51 2
    }
52
53
    /**
54
     * @return bool
55
     */
56 2
    private function memoryLimitAlmostExceeded()
57
    {
58
        return (
59 2
            $this->maxMemoryLimit != '-1' &&
60 2
            memory_get_usage(true) * 1.2 > $this->returnBytes($this->maxMemoryLimit)
61 2
        );
62
    }
63
64
    /**
65
     * @param $value
66
     * @return int
67
     */
68 2
    private function returnBytes($value)
69
    {
70 2
        $value = trim($value);
71 2
        $last = strtolower($value{strlen($value) - 1});
72
73
        switch ($last) {
74 2
            case 'g':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
75 1
                $value *= 1024;
76 2
            case 'm':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
77 1
                $value *= 1024;
78 2
            case 'k':
79 2
                $value *= 1024;
80 2
        }
81
82 2
        return $value;
83
    }
84
85 1
    private function informLogger()
86
    {
87 1
        if (!is_null($this->logger)) {
88
            $this->logger->notice(
89
                sprintf(
90
                    'Memory limit almost exceeded. Max limit is %s, used %s so far.',
91
                    $this->maxMemoryLimit,
92
                    round((memory_get_usage(true) * 1.2) / 1048576, 2) . 'M'
93
                )
94
            );
95
        }
96 1
    }
97
}
98