Completed
Push — master ( 2a7c0c...5b0aeb )
by Iman
06:22
created

ReactionFactory::methodsToCall()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 0
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 3
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 93
    public function __construct(Chain $chain)
20
    {
21 93
        $this->chain = $chain;
22 93
    }
23
24
    /**
25
     * @return \Closure
26
     */
27 93
    public function make(): \Closure
28
    {
29 93
        $reaction = $this->makeReaction();
30
        $cb = $this->chain->predicate;
31 93
32
        return function (...$f) use ($cb, $reaction) {
33
            if (!$cb($f)) {
34
                $reaction();
35
            }
36
        };
37
    }
38
39 93
    private function makeReaction(): \Closure
40
    {
41 93
        $responder = app(ResponderFactory::class)->make();
42
        $beforeResponse = $this->chain->beforeResponse();
43 93
44 93
        return function() use($beforeResponse, $responder){
45
            $beforeResponse();
46
            $responder();
47 63
        };
48 11
    }
49
}
50