Passed
Push — master ( 1ff8c3...d57d17 )
by Iman
03:06 queued 10s
created

ReactionFactory::make()   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
eloc 5
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Imanghafoori\HeyMan\Core;
4
5
use Imanghafoori\HeyMan\Reactions\ResponderFactory;
6
7
final class ReactionFactory
8
{
9 102
    public function make()
10
    {
11 102
        $reaction = $this->makeReaction();
12 102
        $condition = resolve('heyman.chain')->get('condition');
13
14 102
        return function (...$f) use ($condition, $reaction) {
15 71
            if (! $condition($f)) {
16 62
                $reaction();
17
            }
18 102
        };
19
    }
20
21 102
    private function makeReaction()
22
    {
23 102
        $chain = resolve('heyman.chain');
24
25 102
        $beforeReaction = $this->makePreResponseActions($chain);
26
27 102
        $debug = $chain->get('debugInfo') ?? ['file' => '', 'line' => '', 'args' => ''];
28 102
        $termination = $chain->get('termination');
29
30 102
        $responder = resolve(ResponderFactory::class)->make();
31
32 102
        return function () use ($beforeReaction, $responder, $debug, $termination) {
33 62
            if ($termination) {
34 2
                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

34
                app()->/** @scrutinizer ignore-call */ terminating($termination);
Loading history...
35
            }
36 62
            event('heyman_reaction_is_happening', $debug);
37 62
            $beforeReaction();
38 62
            $responder();
39 102
        };
40
    }
41
42 102
    private function makePreResponseActions($chain)
43
    {
44 102
        $tasks = $chain->get('beforeReaction') ?? [];
45 102
        $tasks = $this->convertToClosures($tasks);
46 102
        $beforeReaction = function () use ($tasks) {
47 62
            foreach ($tasks as $task) {
48 3
                $task();
49
            }
50 102
        };
51
52 102
        return $beforeReaction;
53
    }
54
55
    private function convertToClosures($tasks)
56
    {
57 102
        $map = function ($task) {
58 3
            $params = $task[0];
59
60 3
            if ($task[1] == 'event') {
61 1
                return function () use ($params) {
62 1
                    resolve('events')->dispatch(...$params);
0 ignored issues
show
Bug introduced by
$params 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

62
                    resolve('events')->dispatch(/** @scrutinizer ignore-type */ ...$params);
Loading history...
63 1
                };
64
            }
65
66 2
            return function () use ($params) {
67 2
                app()->call(...$params);
0 ignored issues
show
Bug introduced by
$params 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

67
                app()->call(/** @scrutinizer ignore-type */ ...$params);
Loading history...
68 2
            };
69 102
        };
70
71 102
        return array_map($map, $tasks);
72
    }
73
}
74