Issues (45)

src/Core/ReactionFactory.php (3 issues)

1
<?php
2
3
namespace Imanghafoori\HeyMan\Core;
4
5
use Imanghafoori\HeyMan\Reactions\ResponderFactory;
6
7
final class ReactionFactory
8
{
9 106
    public function make()
10
    {
11 106
        $reaction = $this->makeReaction();
12 106
        $condition = resolve('heyman.chain')->get('condition');
13
14 106
        return function (...$f) use ($condition, $reaction) {
15 72
            if (! $condition($f)) {
16 61
                $conditionMeta = resolve('heyman.chain')->get('condition_meta') ?: null;
17 61
                $reaction($conditionMeta);
18
            }
19 106
        };
20
    }
21
22 106
    private function makeReaction()
23
    {
24 106
        $chain = resolve('heyman.chain');
25
26 106
        $beforeReaction = $this->makePreResponseActions($chain);
27
28 106
        $debug = $chain->get('debugInfo') ?? ['file' => '', 'line' => '', 'args' => ''];
29 106
        $termination = $chain->get('termination');
30
31 106
        $responder = resolve(ResponderFactory::class)->make();
32
33 106
        return function ($param = null) use ($beforeReaction, $responder, $debug, $termination) {
34 61
            if ($termination) {
35 2
                app()->terminating($termination);
0 ignored issues
show
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

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

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

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