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

CallbackExtension::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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