Completed
Push — wip/steps ( 1506fd...4499a9 )
by
unknown
08:43
created

StepFetchingMiddleware::after()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.7537
c 0
b 0
f 0
cc 6
nc 7
nop 1
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\Item\Step;
15
16
use Romm\Formz\Form\Definition\Step\Step\StepDefinition;
17
use Romm\Formz\Middleware\Argument\Arguments;
18
use Romm\Formz\Middleware\Item\AbstractMiddleware;
19
use Romm\Formz\Middleware\Item\FormInjection\FormInjectionSignal;
20
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
21
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
22
use Romm\Formz\Middleware\Scope\FieldValidationScope;
23
use Romm\Formz\Middleware\Scope\ReadScope;
24
use Romm\Formz\Middleware\Signal\After;
25
26
/**
27
 * This middleware will fetch the current step in the form, based on the request
28
 * context and the steps definition in the form configuration.
29
 *
30
 * It will check if the user has the right to stand on this step; if not, a loop
31
 * on all previous steps is done to determine the first valid step: the request
32
 * is then redirected to this step.
33
 */
34
class StepFetchingMiddleware extends AbstractMiddleware implements After, FormInjectionSignal, PresetMiddlewareInterface
35
{
36
    /**
37
     * @var int
38
     */
39
    protected $priority = self::PRIORITY_STEP_FETCHING;
40
41
    /**
42
     * @var StepMiddlewareService
43
     */
44
    protected $service;
45
46
    /**
47
     * @var array
48
     */
49
    protected static $defaultScopesBlackList = [ReadScope::class, FieldValidationScope::class];
50
51
    /**
52
     * Inject the step service.
53
     */
54
    public function initializeMiddleware()
55
    {
56
        $this->service = StepMiddlewareService::get();
57
    }
58
59
    /**
60
     * @see StepFetchingMiddleware
61
     *
62
     * @param Arguments $arguments
63
     */
64
    public function after(Arguments $arguments)
65
    {
66
        $formObject = $this->getFormObject();
67
68
        if (false === $formObject->getDefinition()->hasSteps()) {
69
            return;
70
        }
71
72
        $currentStep = $this->getCurrentStep();
73
74
        if ($currentStep) {
75
            $stepDefinition = $this->service->getStepDefinition($currentStep);
76
77
            if (false === $this->service->stepIsValid($stepDefinition)) {
0 ignored issues
show
Bug introduced by
It seems like $stepDefinition defined by $this->service->getStepDefinition($currentStep) on line 75 can be null; however, Romm\Formz\Middleware\It...eService::stepIsValid() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
78
                /*
79
                 * The user has no right to stand on the current step, a previous
80
                 * valid step is determined, and the user is redirected to it.
81
                 */
82
                $stepToRedirect = $this->service->getFirstInvalidStep($currentStep);
83
84
                if ($stepToRedirect instanceof StepDefinition) {
85
                    $this->service->redirectToStep($stepToRedirect->getStep(), $this->redirect());
86
                }
87
88
                /**
89
                 * If we don't find an invalid Step we search the first not validated step
90
                 */
91
                $stepToRedirect = $this->service->getFirstNotValidatedStep($stepDefinition);
0 ignored issues
show
Bug introduced by
It seems like $stepDefinition defined by $this->service->getStepDefinition($currentStep) on line 75 can be null; however, Romm\Formz\Middleware\It...FirstNotValidatedStep() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
92
                if ($stepToRedirect instanceof StepDefinition) {
93
                    $this->service->redirectToStep($stepToRedirect->getStep(), $this->redirect());
94
                }
95
            }
96
        }
97
    }
98
}
99