Completed
Push — feature/middleware ( bf156f...5ee07f )
by Romain
02:27
created

SignalObject   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A withArguments() 0 6 1
A dispatch() 0 12 4
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\Element;
15
16
use Romm\Formz\Middleware\Argument\Arguments;
17
use Romm\Formz\Middleware\Argument\EmptyArguments;
18
use Romm\Formz\Middleware\Processor\MiddlewareProcessor;
19
use Romm\Formz\Middleware\Signal\Before;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
class SignalObject
23
{
24
    /**
25
     * @var MiddlewareProcessor
26
     */
27
    protected $processor;
28
29
    /**
30
     * @var string
31
     */
32
    protected $signal;
33
34
    /**
35
     * @var string
36
     */
37
    protected $type;
38
39
    /**
40
     @var Arguments
41
     */
42
    protected $arguments;
43
44
    /**
45
     * @param MiddlewareProcessor $processor
46
     * @param string              $signal
47
     * @param string              $type
48
     */
49
    public function __construct(MiddlewareProcessor $processor, $signal, $type)
50
    {
51
        $this->processor = $processor;
52
        $this->signal = $signal;
53
        $this->type = $type;
54
        $this->arguments = GeneralUtility::makeInstance(EmptyArguments::class);
55
    }
56
57
    /**
58
     * @param Arguments $arguments
59
     * @return $this
60
     */
61
    public function withArguments(Arguments $arguments)
62
    {
63
        $this->arguments = $arguments;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Will dispatch the configured signal to all middlewares bound to the
70
     * signal name.
71
     */
72
    public function dispatch()
73
    {
74
        foreach ($this->processor->getMiddlewaresBoundToSignal($this->signal) as $middleware) {
75
            if ($middleware instanceof $this->type) {
76
                $method = $this->type === Before::class
77
                    ? 'before'
78
                    : 'after';
79
80
                call_user_func([$middleware, $method], $this->arguments);
81
            }
82
        }
83
    }
84
}
85