Passed
Push — master ( a515c2...9c88e8 )
by Iman
03:59
created

Chain::beforeReaction()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
    public function addCallbackBeforeReaction($callback, $parameters)
25
    {
26 2
        $this->beforeReaction[] = function () use ($callback, $parameters) {
27 2
            app()->call($callback, $parameters);
28 1
        };
29 2
    }
30
31
    public function addEventBeforeReaction($event, array $payload, bool $halt)
32
    {
33 1
        $this->beforeReaction[] = function () use ($event, $payload, $halt) {
34 1
            resolve('events')->dispatch($event, $payload, $halt);
35 1
        };
36 1
    }
37
38 94
    public function submitChainConfig()
39
    {
40 94
        $callbackListener = resolve(ReactionFactory::class)->make();
41 94
        $this->eventManager->commitChain($callbackListener);
42 94
    }
43
44 94
    public function beforeReaction(): \Closure
45
    {
46 94
        $tasks = $this->beforeReaction;
47 94
        $this->beforeReaction = [];
48
49 94
        return function () use ($tasks) {
50 54
            foreach ($tasks as $task) {
51 3
                $task();
52
            }
53 94
        };
54
    }
55
56 88
    public function commitCalledMethod($args, $methodName)
57
    {
58 88
        $this->data[] = $args;
59 88
        $this->responseType = $methodName;
60 88
    }
61
62
    /**
63
     * @param $d
64
     */
65 103
    public function writeDebugInfo($d)
66
    {
67 103
        $this->debugInfo = array_only($d[1], ['file', 'line', 'args']);
68 103
    }
69
}
70