Passed
Push — master ( b8bbb5...e96284 )
by Iman
09:40
created

ReactionFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A methodsToCall() 0 12 3
A make() 0 5 1
A eventsToDispatch() 0 12 3
A makeReaction() 0 16 2
1
<?php
2
3
namespace Imanghafoori\HeyMan;
4
5
class ReactionFactory
6
{
7
    /**
8
     * @var \Imanghafoori\HeyMan\Chain
9
     */
10
    private $chain;
11
12
    /**
13
     * ListenerFactory constructor.
14
     *
15
     * @param \Imanghafoori\HeyMan\Chain $chain
16
     */
17
    public function __construct(Chain $chain)
18
    {
19
        $this->chain = $chain;
20
    }
21
22
    /**
23
     * @return \Closure
24
     */
25
    public function make(): \Closure
26
    {
27
        $responder = app(ResponderFactory::class)->make();
28
29
        return $this->makeReaction($responder);
30
    }
31
32
    /**
33
     * @param $responder
34
     *
35
     * @return \Closure
36
     */
37
    private function makeReaction(callable $responder): \Closure
38
    {
39
        $dispatcher = $this->eventsToDispatch();
40
        $calls = $this->methodsToCall();
41
42
        $cb = $this->chain->predicate;
43
        $this->chain->reset();
44
45
        return function (...$f) use ($responder, $cb, $dispatcher, $calls) {
46
            if ($cb($f)) {
47
                return true;
48
            }
49
50
            $calls();
51
            $dispatcher();
52
            $responder();
53
        };
54
    }
55
56
    private function eventsToDispatch(): \Closure
57
    {
58
        $events = $this->chain->events;
59
60
        if (!$events) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $events of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
61
            return function () {
62
            };
63
        }
64
65
        return function () use ($events) {
66
            foreach ($events as $event) {
67
                app('events')->dispatch(...$event);
0 ignored issues
show
Bug introduced by
$event 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

67
                app('events')->dispatch(/** @scrutinizer ignore-type */ ...$event);
Loading history...
68
            }
69
        };
70
    }
71
72
    private function methodsToCall(): \Closure
73
    {
74
        $calls = $this->chain->afterCalls;
75
76
        if (!$calls) {
77
            return function () {
78
            };
79
        }
80
81
        return function () use ($calls) {
82
            foreach ($calls as $call) {
83
                app()->call(...$call);
0 ignored issues
show
Bug introduced by
$call 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

83
                app()->call(/** @scrutinizer ignore-type */ ...$call);
Loading history...
84
            }
85
        };
86
    }
87
}
88