Completed
Push — middleware-wip-tmp2 ( a3478c...ef0a86 )
by Romain
05:09
created

StepDispatchingMiddleware::getFormRawValues()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
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\Middleware\Item\DefaultMiddleware;
17
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
18
use Romm\Formz\Middleware\State\PresetMiddlewareInterface;
19
20
/**
21
 * This middleware should be the last one called, as it is used to dispatch the
22
 * request to the next step, if there is one.
23
 */
24
class StepDispatchingMiddleware extends DefaultMiddleware implements PresetMiddlewareInterface
25
{
26
    /**
27
     * @var int
28
     */
29
    protected $priority = self::PRIORITY_STEP;
30
31
    /**
32
     * @var StepMiddlewareService
33
     */
34
    protected $service;
35
36
    /**
37
     * Inject the step service.
38
     */
39
    public function initializeMiddleware()
40
    {
41
        $this->service = StepMiddlewareService::get();
42
    }
43
44
    /**
45
     * @see StepDispatchingMiddleware
46
     */
47
    protected function process()
48
    {
49
        $formObject = $this->getFormObject();
50
        $result = $formObject->getFormResult();
51
52
        if ($formObject->getConfiguration()->hasSteps()
53
            && $formObject->formWasSubmitted()
54
            && false === $result->hasErrors()
55
        ) {
56
            $currentStep = $this->getCurrentStep();
57
            $currentStepDefinition = $this->service->getStepDefinition($currentStep);
0 ignored issues
show
Bug introduced by
It seems like $currentStep defined by $this->getCurrentStep() on line 56 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...
58
59
            $this->service->markStepAsValidated($currentStep, $this->getFormRawValues());
0 ignored issues
show
Bug introduced by
It seems like $currentStep defined by $this->getCurrentStep() on line 56 can be null; however, Romm\Formz\Middleware\It...::markStepAsValidated() 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...
60
61
            if ($currentStepDefinition->hasNextSteps()) {
62
                $nextStep = $currentStepDefinition
63
                    ->getNextSteps()
64
                    ->getDefaultStep()
65
                    ->getStep();
66
67
                $this->service->redirectToStep($nextStep, $this->redirect());
68
            }
69
        }
70
    }
71
72
    /**
73
     * Fetches the raw values sent in the request.
74
     *
75
     * @return array
76
     */
77
    protected function getFormRawValues()
78
    {
79
        $formName = $this->getFormObject()->getName();
80
        $formArray = null;
81
82
        if ($this->getRequest()->hasArgument($formName)) {
83
            /** @var array $formArray */
84
            $formArray = $this->getRequest()->getArgument($formName);
85
        }
86
87
        if (false === is_array($formArray)) {
88
            throw new \Exception('todo'); // @todo
89
        }
90
91
        return $formArray;
92
    }
93
}
94