Passed
Push — master ( f43d04...c61266 )
by Iman
06:27
created

ReactionFactory::makePreResponseActions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 1
crap 6
1
<?php
2
3
namespace Imanghafoori\HeyMan\Reactions;
4
5
use Imanghafoori\HeyMan\ChainManager;
6
7
final class ReactionFactory
8
{
9
    /**
10
     * @return \Closure
11
     */
12 96
    public function make(): \Closure
13
    {
14 96
        $reaction = $this->makeReaction();
15 96
        $condition = resolve(ChainManager::class)->get('condition');
16
17 96
        return function (...$f) use ($condition, $reaction) {
18 67
            if (!$condition($f)) {
19 56
                $reaction();
20
            }
21 96
        };
22
    }
23
24 96
    private function makeReaction(): \Closure
25
    {
26 96
        $chain = resolve(ChainManager::class);
27 96
28 96
        $beforeReaction = $this->makePreResponseActions($chain);
29 96
30
        $debug = $chain->get('debugInfo');
31 96
        $termination = $chain->get('termination');
32
33 96
        $responder = resolve(ResponderFactory::class)->make();
34 56
35 1
        return function () use ($beforeReaction, $responder, $debug, $termination) {
36
            if ($termination) {
37 56
                app()->terminating($termination);
0 ignored issues
show
introduced by
The method terminating() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
                app()->/** @scrutinizer ignore-call */ terminating($termination);
Loading history...
38 56
            }
39 55
            event('heyman_reaction_is_happening', $debug);
40 96
            $beforeReaction();
41
            $responder();
42
        };
43
    }
44
45
    /**
46
     * @param $chain
47
     * @return \Closure
48
     */
49
    private function makePreResponseActions($chain): \Closure
50
    {
51
        $tasks = $this->convertToClosures($chain);
52
        $beforeReaction = function () use ($tasks) {
53
            foreach ($tasks as $task) {
54
                $task();
55
            }
56
        };
57
58
        return $beforeReaction;
59
    }
60
61
    /**
62
     * @param $chain
63
     * @return array
64
     */
65
    private function convertToClosures($chain): array
66
    {
67
        $tasks = $chain->get('beforeReaction');
68
        $r = [];
69
        foreach ($tasks as $task) {
70
            if ($task[1] == 'event') {
71
72
                $r[] = function () use ($task) {
73
                    resolve('events')->dispatch(...$task[0]);
0 ignored issues
show
Bug introduced by
$task[0] is expanded, but the parameter $event of Illuminate\Events\Dispatcher::dispatch() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
                    resolve('events')->dispatch(/** @scrutinizer ignore-type */ ...$task[0]);
Loading history...
74
                };
75
            } elseif ($task[1] == 'cb') {
76
                $r[] = function () use ($task) {
77
                    app()->call(...$task[0]);
0 ignored issues
show
Bug introduced by
$task[0] is expanded, but the parameter $callback of Illuminate\Container\Container::call() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
                    app()->call(/** @scrutinizer ignore-type */ ...$task[0]);
Loading history...
78
                };
79
            }
80
        }
81
        return $r;
82
    }
83
}
84