Passed
Push — master ( 76636e...a7cce5 )
by Kevin
03:12
created

CallbackExtension   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 22
c 1
b 0
f 0
dl 0
loc 85
ccs 33
cts 35
cp 0.9429
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A scheduleBefore() 0 3 1
A taskFilter() 0 3 1
A taskFailure() 0 3 1
A taskBefore() 0 3 1
A scheduleSuccess() 0 3 1
A scheduleFilter() 0 3 1
A getCallback() 0 3 1
A scheduleAfter() 0 3 1
A taskSuccess() 0 3 1
A taskAfter() 0 3 1
A __construct() 0 4 1
A scheduleFailure() 0 3 1
A getHook() 0 3 1
A createDescriptionFromCallback() 0 9 2
A __toString() 0 3 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Extension;
4
5
use Zenstruck\ScheduleBundle\Schedule;
6
use Zenstruck\ScheduleBundle\Schedule\Task;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
final class CallbackExtension
12
{
13
    private $hook;
14
    private $callback;
15
16 25
    private function __construct(string $hook, callable $callback)
17
    {
18 25
        $this->hook = $hook;
19 25
        $this->callback = $callback;
20 25
    }
21
22
    public function __toString(): string
23
    {
24
        return \sprintf('%s callback: %s', $this->hook, self::createDescriptionFromCallback($this->callback));
25
    }
26
27 25
    public function getHook(): string
28
    {
29 25
        return $this->hook;
30
    }
31
32 25
    public function getCallback(): callable
33
    {
34 25
        return $this->callback;
35
    }
36
37 12
    public static function taskFilter(callable $callback): self
38
    {
39 12
        return new self(Task::FILTER, $callback);
40
    }
41
42 3
    public static function taskBefore(callable $callback): self
43
    {
44 3
        return new self(Task::BEFORE, $callback);
45
    }
46
47 3
    public static function taskAfter(callable $callback): self
48
    {
49 3
        return new self(Task::AFTER, $callback);
50
    }
51
52 3
    public static function taskSuccess(callable $callback): self
53
    {
54 3
        return new self(Task::SUCCESS, $callback);
55
    }
56
57 3
    public static function taskFailure(callable $callback): self
58
    {
59 3
        return new self(Task::FAILURE, $callback);
60
    }
61
62 13
    public static function scheduleFilter(callable $callback): self
63
    {
64 13
        return new self(Schedule::FILTER, $callback);
65
    }
66
67 3
    public static function scheduleBefore(callable $callback): self
68
    {
69 3
        return new self(Schedule::BEFORE, $callback);
70
    }
71
72 3
    public static function scheduleAfter(callable $callback): self
73
    {
74 3
        return new self(Schedule::AFTER, $callback);
75
    }
76
77 3
    public static function scheduleSuccess(callable $callback): self
78
    {
79 3
        return new self(Schedule::SUCCESS, $callback);
80
    }
81
82 3
    public static function scheduleFailure(callable $callback): self
83
    {
84 3
        return new self(Schedule::FAILURE, $callback);
85
    }
86
87 15
    public static function createDescriptionFromCallback(callable $callback): string
88
    {
89 15
        $ref = new \ReflectionFunction(\Closure::fromCallable($callback));
90
91 15
        if ($class = $ref->getClosureScopeClass()) {
92 15
            return "{$class->getName()}:{$ref->getStartLine()}";
93
        }
94
95 1
        return $ref->getName();
96
    }
97
}
98