Completed
Push — master ( c3e82e...f2093f )
by Iman
08:39
created

Chain::getTermination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Imanghafoori\HeyMan;
4
5
use Imanghafoori\HeyMan\Reactions\ReactionFactory;
6
7
class Chain
8
{
9
    /**
10
     * @var \Imanghafoori\HeyMan\ListenerApplier
0 ignored issues
show
Bug introduced by
The type Imanghafoori\HeyMan\ListenerApplier was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
     */
12
    public $eventManager;
13
14
    public $debugInfo;
15
16
    public $condition;
17
18
    public $responseType = 'nothing';
19
20
    public $data = [];
21
22
    private $beforeReaction = [];
23
24
    private $termination;
25
26 2
    public function addCallbackBeforeReaction($callback, $parameters)
27 2
    {
28 1
        $this->beforeReaction[] = function () use ($callback, $parameters) {
29 2
            app()->call($callback, $parameters);
30
        };
31
    }
32
33 1
    public function addEventBeforeReaction($event, array $payload, bool $halt)
34 1
    {
35 1
        $this->beforeReaction[] = function () use ($event, $payload, $halt) {
36 1
            resolve('events')->dispatch($event, $payload, $halt);
37
        };
38 96
    }
39
40 96
    public function addTerminationCallback($callback)
41 96
    {
42 96
        $this->termination = $callback;
43
    }
44 96
45
    public function submitChainConfig()
46 96
    {
47 96
        $callbackListener = resolve(ReactionFactory::class)->make();
48
        $this->eventManager->commitChain($callbackListener);
49 96
    }
50 56
51 3
    public function beforeReaction(): \Closure
52
    {
53 96
        $tasks = $this->beforeReaction;
54
        $this->beforeReaction = [];
55
56 90
        return function () use ($tasks) {
57
            foreach ($tasks as $task) {
58 90
                $task();
59 90
            }
60 90
        };
61
    }
62
63
    public function commitCalledMethod($args, $methodName)
64
    {
65 105
        $this->data[] = $args;
66
        $this->responseType = $methodName;
67 105
    }
68 105
69
    /**
70
     * @param $d
71
     */
72
    public function writeDebugInfo($d)
73
    {
74
        $this->debugInfo = array_only($d[1], ['file', 'line', 'args']);
75
    }
76
77
    public function getTermination()
78
    {
79
        $r = $this->termination;
80
        $this->termination = null;
81
82
        return $r;
83
    }
84
}
85