CallbackExtension   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 88
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 88
ccs 33
cts 35
cp 0.9429
rs 10

15 Methods

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