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

dispatchBeforeSignal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
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\State;
15
16
use Romm\Formz\Middleware\Signal\After;
17
use Romm\Formz\Middleware\Signal\Before;
18
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal;
19
use Romm\Formz\Middleware\Signal\SignalObject;
20
21
trait StateMiddlewareDependencyTrait
22
{
23
    /**
24
     * @var MiddlewareState
25
     */
26
    private $state;
27
28
    /**
29
     * @see \Romm\Formz\Middleware\Signal\SendsMiddlewareSignal::beforeSignal()
30
     *
31
     * @param string $signal
32
     * @return SignalObject
33
     */
34
    final public function beforeSignal($signal = null)
35
    {
36
        return $this->getSignalObject($signal, Before::class);
37
    }
38
39
    /**
40
     * @see \Romm\Formz\Middleware\Signal\SendsMiddlewareSignal::afterSignal()
41
     *
42
     * @param string $signal
43
     * @return SignalObject
44
     */
45
    final public function afterSignal($signal = null)
46
    {
47
        return $this->getSignalObject($signal, After::class);
48
    }
49
50
    /**
51
     * @param string $signal
52
     * @param string $type
53
     * @return SignalObject
54
     */
55
    private function getSignalObject($signal, $type)
56
    {
57
        if (false === $this instanceof SendsMiddlewareSignal) {
58
            // @todo exception
59
            throw new \Exception();
60
        }
61
62
        if (null === $signal) {
63
            if (count($this->getAllowedSignals()) > 1) {
64
                // @todo exception
65
                throw new \Exception();
66
            }
67
68
            $signal = reset($this->getAllowedSignals());
0 ignored issues
show
Bug introduced by
$this->getAllowedSignals() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
69
        }
70
71
        if (false === in_array($signal, $this->getAllowedSignals())) {
72
            // @todo exception
73
            throw new \Exception();
74
        }
75
76
        return new SignalObject($this->state, $signal, $type);
0 ignored issues
show
Bug introduced by
Accessing state on the interface Romm\Formz\Middleware\Signal\SendsMiddlewareSignal suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
77
    }
78
79
    /**
80
     * @param MiddlewareState $middlewareState
81
     */
82
    final public function bindMiddlewareState(MiddlewareState $middlewareState)
83
    {
84
        $this->state = $middlewareState;
85
    }
86
}
87