Completed
Push — wip/steps ( 47ee47 )
by Romain
03:13
created

StepFetchingMiddleware::before()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 12
nc 4
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\Step;
17
use Romm\Formz\Form\Definition\Step\Step\StepDefinition;
18
use Romm\Formz\Middleware\Argument\Arguments;
19
use Romm\Formz\Middleware\Item\AbstractMiddleware;
20
use Romm\Formz\Middleware\Item\FormValidation\FormValidationSignal;
21
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
22
use Romm\Formz\Middleware\Signal\Before;
23
24
/**
25
 * This middleware will fetch the current step in the form, based on the request
26
 * context and the steps definition in the form configuration.
27
 *
28
 * It will check if the user has the right to stand on this step; if not, a loop
29
 * on all previous steps is done to determine the first valid step: the request
30
 * is then redirected to this step.
31
 */
32
class StepFetchingMiddleware extends AbstractMiddleware implements Before, FormValidationSignal
33
{
34
    /**
35
     * @var int
36
     */
37
    protected $priority = self::PRIORITY_STEP;
38
39
    /**
40
     * @var StepMiddlewareService
41
     */
42
    protected $service;
43
44
    /**
45
     * Inject the step service.
46
     */
47
    public function initializeMiddleware()
48
    {
49
        $this->service = StepMiddlewareService::get();
50
    }
51
52
    /**
53
     * @see StepFetchingMiddleware
54
     *
55
     * @param Arguments $arguments
56
     */
57
    public function before(Arguments $arguments)
58
    {
59
        $formObject = $this->getFormObject();
60
        $this->service->reset($formObject);
61
62
        if (false === $formObject->getDefinition()->hasSteps()) {
63
            return;
64
        }
65
66
        $currentStep = $this->getCurrentStep();
67
        $stepDefinition = $this->service->getStepDefinition($currentStep);
0 ignored issues
show
Bug introduced by
It seems like $currentStep defined by $this->getCurrentStep() on line 66 can be null; however, Romm\Formz\Middleware\It...ce::getStepDefinition() 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...
68
69
        if ($currentStep
70
            && false === $this->service->stepIsValid($stepDefinition)
0 ignored issues
show
Bug introduced by
It seems like $stepDefinition defined by $this->service->getStepDefinition($currentStep) on line 67 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...
71
        ) {
72
            /*
73
             * The user has no right to stand on the current step, a previous
74
             * valid step is determined, and the user is redirected to it.
75
             */
76
            $stepToRedirect = $this->service->getFirstInvalidStep($currentStep);
77
78
            if ($stepToRedirect instanceof StepDefinition) {
79
                $this->service->redirectToStep($stepToRedirect->getStep(), $this->redirect());
80
            }
81
        }
82
    }
83
}
84