Completed
Push — middleware-wip ( ab6ae7...17e892 )
by Romain
02:53
created

SignalObject::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Middleware\Signal;
15
16
use Romm\Formz\Middleware\Arguments\Arguments;
17
use Romm\Formz\Middleware\Arguments\EmptyArguments;
18
use Romm\Formz\Middleware\State\MiddlewareState;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
class SignalObject
22
{
23
    /**
24
     * @var MiddlewareState
25
     */
26
    protected $state;
27
28
    /**
29
     * @var string
30
     */
31
    protected $signal;
32
33
    /**
34
     * @var string
35
     */
36
    protected $type;
37
38
    /**
39
     @var Arguments
40
     */
41
    protected $arguments;
42
43
    /**
44
     * @param MiddlewareState $state
45
     * @param string          $signal
46
     * @param string          $type
47
     */
48
    public function __construct(MiddlewareState $state, $signal, $type)
49
    {
50
        $this->state = $state;
51
        $this->signal = $signal;
52
        $this->type = $type;
53
        $this->arguments = GeneralUtility::makeInstance(EmptyArguments::class);
54
    }
55
56
    /**
57
     * @param Arguments $arguments
58
     * @return $this
59
     */
60
    public function withArguments(Arguments $arguments)
61
    {
62
        $this->arguments = $arguments;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Will dispatch the configured signal to all middlewares bound to the
69
     * signal name.
70
     */
71
    public function dispatch()
72
    {
73
        foreach ($this->state->getMiddlewaresBoundToSignal($this->signal) as $middleware) {
74
            if ($middleware instanceof $this->type) {
75
                $method = $this->type === Before::class
76
                    ? 'before'
77
                    : 'after';
78
79
                call_user_func([$middleware, $method], $this->arguments);
80
            }
81
        }
82
    }
83
}
84