Completed
Push — master ( cd5a9f...12dfc9 )
by Iman
03:04
created

ReactionFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\HeyMan\Reactions;
4
5
use Imanghafoori\HeyMan\Chain;
6
7
class ReactionFactory
8
{
9
    /**
10
     * @var \Imanghafoori\HeyMan\Chain
11
     */
12
    private $chain;
13
14
    /**
15
     * ListenerFactory constructor.
16
     *
17
     * @param Chain $chain
18
     */
19 78
    public function __construct(Chain $chain)
20
    {
21 78
        $this->chain = $chain;
22 78
    }
23
24
    /**
25
     * @return \Closure
26
     */
27 78
    public function make(): \Closure
28
    {
29 78
        $responder = app(ResponderFactory::class)->make();
30
31 78
        return $this->makeReaction($responder);
32
    }
33
34
    /**
35
     * @param $responder
36
     *
37
     * @return \Closure
38
     */
39 78
    private function makeReaction(callable $responder): \Closure
40
    {
41 78
        $beforeResponse = $this->methodsToCall();
42
43 78
        $cb = $this->chain->predicate;
44 78
        $this->chain->reset();
45
46
        return function (...$f) use ($responder, $cb, $beforeResponse) {
47 60
            if ($cb($f)) {
48 11
                return true;
49
            }
50
51 49
            $beforeResponse();
52 48
            $responder();
53 78
        };
54
    }
55
56 78
    private function methodsToCall(): \Closure
57
    {
58 78
        $calls = $this->chain->beforeResponse;
59
60 78
        if (!$calls) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $calls 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 75
            };
63
        }
64
65
        return function () use ($calls) {
66 3
            foreach ($calls as $call) {
67 3
                $call();
68
            }
69 3
        };
70
    }
71
}
72