Passed
Push — master ( c61266...84ac3f )
by Iman
11:29 queued 08:42
created

ReactionFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
eloc 32
dl 0
loc 77
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 8 2
A convertToClosures() 0 17 4
A makeReaction() 0 18 2
A makePreResponseActions() 0 10 2
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
28 96
        $beforeReaction = $this->makePreResponseActions($chain);
29
30 96
        $debug = $chain->get('debugInfo');
31 96
        $termination = $chain->get('termination');
32
33 96
        $responder = resolve(ResponderFactory::class)->make();
34
35 96
        return function () use ($beforeReaction, $responder, $debug, $termination) {
36 56
            if ($termination) {
37 1
                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
            }
39 56
            event('heyman_reaction_is_happening', $debug);
40 56
            $beforeReaction();
41 55
            $responder();
42 96
        };
43
    }
44
45
    /**
46
     * @param $chain
47
     *
48
     * @return \Closure
49
     */
50 96
    private function makePreResponseActions($chain): \Closure
51
    {
52 96
        $tasks = $this->convertToClosures($chain);
53 96
        $beforeReaction = function () use ($tasks) {
54 56
            foreach ($tasks as $task) {
55 3
                $task();
56
            }
57 96
        };
58
59 96
        return $beforeReaction;
60
    }
61
62
    /**
63
     * @param $chain
64
     *
65
     * @return array
66
     */
67 96
    private function convertToClosures($chain): array
68
    {
69 96
        $tasks = $chain->get('beforeReaction');
70 96
        $r = [];
71 96
        foreach ($tasks as $task) {
72 3
            if ($task[1] == 'event') {
73 1
                $r[] = function () use ($task) {
74 1
                    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

74
                    resolve('events')->dispatch(/** @scrutinizer ignore-type */ ...$task[0]);
Loading history...
75 1
                };
76 2
            } elseif ($task[1] == 'cb') {
77 3
                $r[] = function () use ($task) {
78 2
                    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

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