Completed
Push — feature/middleware ( 864c18 )
by Romain
03:21
created

MiddlewareIsValidValidator::checkImplementation()   F

Complexity

Conditions 15
Paths 361

Size

Total Lines 100
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 3.7313
c 0
b 0
f 0
cc 15
eloc 62
nc 361
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Validation\Validator\Internal;
15
16
use Romm\Formz\Middleware\Item\Begin\BeginSignal;
17
use Romm\Formz\Middleware\Item\End\EndSignal;
18
use Romm\Formz\Middleware\MiddlewareInterface;
19
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
20
use Romm\Formz\Middleware\Signal\After;
21
use Romm\Formz\Middleware\Signal\Before;
22
use Romm\Formz\Middleware\Signal\MiddlewareSignal;
23
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
24
25
class MiddlewareIsValidValidator extends AbstractValidator
26
{
27
    /**
28
     * @param string $middleware
29
     */
30
    public function isValid($middleware)
31
    {
32
        $this->checkImplementation($middleware);
33
    }
34
35
    /**
36
     * @param string $middleware
37
     */
38
    protected function checkImplementation($middleware)
39
    {
40
        if (false === class_exists($middleware)) {
41
            $this->addError(
42
                'Class name given was not found: "%s".',
43
                1489070202,
44
                [$middleware]
45
            );
46
        } else {
47
            $interfaces = class_implements($middleware);
48
49
            if (false === in_array(MiddlewareInterface::class, $interfaces)) {
50
                $this->addError(
51
                    'Class "%s" must implement "%s".',
52
                    1489070282,
53
                    [$middleware, MiddlewareInterface::class]
54
                );
55
            }
56
57
            if (in_array(PresetMiddlewareInterface::class, $interfaces)) {
58
                $this->addError(
59
                    'You can not register a FormZ preset middleware (%s), check the `presetMiddlewares` configuration.',
60
                    1490967642,
61
                    [$middleware]
62
                );
63
            }
64
65
            $signalsFound = [];
66
            foreach ($interfaces as $interface) {
67
                if (in_array(MiddlewareSignal::class, class_implements($interface))) {
68
                    $signalsFound[] = $interface;
69
                }
70
            }
71
72
            if (empty($signalsFound)) {
73
                $this->addError(
74
                    'Class "%s" must implement one interface that extends "%s".',
75
                    1489074248,
76
                    [$middleware, MiddlewareSignal::class]
77
                );
78
            } elseif (count($signalsFound) > 1) {
79
                $this->addError(
80
                    'Class "%s" must implement only one interface that extends "%s"; %s were found: "%s"',
81
                    1489074852,
82
                    [
83
                        $middleware,
84
                        MiddlewareSignal::class,
85
                        count($signalsFound),
86
                        implode('", "', $signalsFound)
87
                    ]
88
                );
89
            }
90
91
            if (false === in_array(Before::class, $interfaces)
92
                && false === in_array(After::class, $interfaces)
93
            ) {
94
                $this->addError(
95
                    'Class "%s" must implement at least one of these interfaces: "%s", "%s".',
96
                    1489074986,
97
                    [
98
                        $middleware,
99
                        Before::class,
100
                        After::class
101
                    ]
102
                );
103
            }
104
105
            if (false === $this->result->hasErrors()) {
106
                if (in_array(BeginSignal::class, $interfaces)
107
                    && in_array(Before::class, $interfaces)
108
                ) {
109
                    $this->addError(
110
                        'Class "%s" implements interfaces "%s" and "%s", but the signal "before beginning" is (obviously) invalid. Please remove "%s" dependency.',
111
                        1489075185,
112
                        [
113
                            $middleware,
114
                            Before::class,
115
                            BeginSignal::class,
116
                            Before::class,
117
                        ]
118
                    );
119
                }
120
121
                if (in_array(EndSignal::class, $interfaces)
122
                    && in_array(After::class, $interfaces)
123
                ) {
124
                    $this->addError(
125
                        'Class "%s" implements interfaces "%s" and "%s", but the signal "after the end" is (obviously) invalid. Please remove "%s" dependency.',
126
                        1489075242,
127
                        [
128
                            $middleware,
129
                            After::class,
130
                            EndSignal::class,
131
                            After::class,
132
                        ]
133
                    );
134
                }
135
            }
136
        }
137
    }
138
}
139