Completed
Push — wip/steps ( d97a08...f0546c )
by Romain
03:30
created

StepDispatchingMiddleware::process()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 8.6737
c 1
b 0
f 0
cc 5
eloc 11
nc 4
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\Item\Step;
15
16
use Romm\Formz\Exceptions\InvalidArgumentTypeException;
17
use Romm\Formz\Form\FormObject\FormObjectFactory;
18
use Romm\Formz\Middleware\Item\DefaultMiddleware;
19
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
20
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
21
22
/**
23
 * This middleware should be the last one called, as it is used to dispatch the
24
 * request to the next step, if there is one.
25
 */
26
class StepDispatchingMiddleware extends DefaultMiddleware implements PresetMiddlewareInterface
27
{
28
    /**
29
     * @var int
30
     */
31
    protected $priority = self::PRIORITY_STEP;
32
33
    /**
34
     * @var StepMiddlewareService
35
     */
36
    protected $service;
37
38
    /**
39
     * Inject the step service.
40
     */
41
    public function initializeMiddleware()
42
    {
43
        $this->service = StepMiddlewareService::get();
44
    }
45
46
    /**
47
     * @see StepDispatchingMiddleware
48
     */
49
    protected function process()
50
    {
51
        $formObject = $this->getFormObject();
52
53
        if (false === $formObject->getDefinition()->hasSteps()) {
54
            return;
55
        }
56
57
        $formResult = $formObject->getFormResult();
58
59
        if ($formObject->formWasSubmitted()
60
            && false === $formResult->hasErrors()
61
        ) {
62
            $stepService = FormObjectFactory::get()->getStepService($formObject);
63
64
            if (false === $stepService->lastSubstepWasValidated()) {
65
                return;
66
            }
67
68
            $this->service->redirectToNextStep($this->getCurrentStep(), $this->redirect());
0 ignored issues
show
Bug introduced by
It seems like $this->getCurrentStep() can be null; however, redirectToNextStep() 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...
69
        }
70
    }
71
}
72