Completed
Branch dev (ed868c)
by Raffael
03:59
created

EventsTrait::on()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * TaskScheduler
7
 *
8
 * @author      Raffael Sahli <[email protected]>
9
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
10
 * @license     MIT https://opensource.org/licenses/MIT
11
 */
12
13
namespace TaskScheduler;
14
15
use Closure;
16
use League\Event\Emitter;
17
18
trait EventsTrait
19
{
20
    /**
21
     * Emitter
22
     *
23
     * @var Emitter
24
     */
25
    protected $emitter;
26
27
    /**
28
     * Bind event listener
29
     */
30
    public function on(string $event, Closure $handler)
31
    {
32
        if(!in_array($event, Scheduler::VALID_EVENTS)) {
33
            $name = 'taskscheduler.on'.ucfirst($event);
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
34
        }
35
36
        $this->emitter->addListener($event, $handler);
37
        return $this;
38
    }
39
40
    /**
41
     * Emit process event
42
     */
43 3
    protected function emit(Process $process): bool
44
    {
45 3
        switch ($process->getStatus()) {
46 3
             case JobInterface::STATUS_WAITING:
47 3
                $this->emitter->emit('taskscheduler.onWaiting', $process);
48 3
                return true;
49 2
             case JobInterface::STATUS_PROCESSING:
50 2
                $this->emitter->emit('taskscheduler.onStart', $process);
51 2
                return true;
52 2
             case JobInterface::STATUS_DONE:
53 1
                $this->emitter->emit('taskscheduler.onDone', $process);
54 1
                return true;
55 1
             case JobInterface::STATUS_POSTPONED:
56
                $this->emitter->emit('taskscheduler.onPostponed', $process);
57
                return true;
58 1
             case JobInterface::STATUS_FAILED:
59 1
                $this->emitter->emit('taskscheduler.onFailed', $process);
60 1
                return true;
61
             case JobInterface::STATUS_TIMEOUT:
62
                $this->emitter->emit('taskscheduler.onTimeout', $process);
63
                return true;
64
             case JobInterface::STATUS_CANCELED:
65
                $this->emitter->emit('taskscheduler.onCancel', $process);
66
                return true;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
67
        }
68
    }
69
}
70