CallbackExtension::createDescriptionFromCallback()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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