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.

MaxRuntime::onBeforeRun()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
ccs 10
cts 10
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 6
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 MaxRuntime implements EventSubscriberInterface
10
{
11
    /**
12
     * @var \DateTime
13
     */
14
    private $startTime;
15
    /**
16
     * @var string Datetime modify string
17
     */
18
    private $maxRunTime;
19
20
    /**
21
     * @var LoggerInterface
22
     */
23
    private $logger;
24
25
26 3
    public function __construct($maxRuntime = '+2 hours', LoggerInterface $logger = null)
27
    {
28 3
        $this->startTime = new \DateTime();
29 3
        $this->maxRunTime = $maxRuntime;
30 3
        $this->logger = $logger;
31 3
    }
32
33
    public static function getSubscribedEvents()
34
    {
35
        return array(
36
            WorkerEvent::EVENT_BEFORE_RUN => array('onBeforeRun'),
37
            WorkerEvent::EVENT_AFTER_RUN => array('onAfterRun'),
38
            WorkerEvent::EVENT_ON_NO_JOBS => array('onNoJobs'),
39
        );
40
    }
41
42 1
    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...
43
    {
44 1
        if (!is_null($this->logger)) {
45 1
            $this->logger->notice(
46 1
                sprintf(
47 1
                    'If the max runtime of %s is exceeded, i will stop the worker.',
48 1
                    $this->maxRunTime
49 1
                )
50 1
            );
51 1
        }
52 1
    }
53
54 2
    public function onNoJobs(WorkerEvent $event)
55
    {
56 2
        if ($this->timeIsExceeded()) {
57 1
            $this->informLogger();
58
59 1
            $event->getWorkerInstance()->destroyWorker();
60 1
        }
61 2
    }
62
63 2
    public function onAfterRun(WorkerEvent $event)
64
    {
65 2
        if ($this->timeIsExceeded()) {
66 1
            $this->informLogger();
67
68 1
            $event->getWorkerInstance()->destroyWorker();
69 1
        }
70 2
    }
71
72 2
    private function timeIsExceeded()
73
    {
74
        /** @var \DateTime $afterTime */
75 2
        $afterTime = clone $this->startTime;
76 2
        $afterTime->modify($this->maxRunTime);
77
78 2
        $now = new \DateTime();
79
80 2
        return ($now > $afterTime);
81
    }
82
83 1
    private function informLogger()
84
    {
85 1
        if (!is_null($this->logger)) {
86 1
            $this->logger->notice(
87 1
                sprintf(
88 1
                    'Max Runtime exceeded. Started at %s, checked last at %s. Maxruntime is %s',
89 1
                    $this->startTime->format('d.m.Y H:i:s'),
90 1
                    date('d.m.Y H:i:s'),
91 1
                    $this->maxRunTime
92 1
                )
93 1
            );
94 1
        }
95 1
    }
96
}
97