Completed
Push — wip/steps ( b7e8e2...f18ed0 )
by Romain
05:33
created

SubstepFetchingMiddleware::after()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Romm\Formz\Middleware\Item\Step;
5
6
use Romm\Formz\Form\Definition\Step\Step\Step;
7
use Romm\Formz\Form\FormObject\FormObjectFactory;
8
use Romm\Formz\Form\FormObject\Service\FormObjectSteps;
9
use Romm\Formz\Middleware\Argument\Arguments;
10
use Romm\Formz\Middleware\Item\AbstractMiddleware;
11
use Romm\Formz\Middleware\Item\FormInjection\FormInjectionSignal;
12
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
13
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
14
use Romm\Formz\Middleware\Signal\After;
15
16
class SubstepFetchingMiddleware extends AbstractMiddleware implements After, FormInjectionSignal, PresetMiddlewareInterface
17
{
18
    /**
19
     * @var int
20
     */
21
    protected $priority = self::PRIORITY_STEP_FETCHING + 150;
22
23
    /**
24
     * @var Step
25
     */
26
    protected $currentStep;
27
28
    /**
29
     * @var StepMiddlewareService
30
     */
31
    protected $service;
32
33
    /**
34
     * @var FormObjectSteps
35
     */
36
    protected $stepService;
37
38
    /**
39
     * Inject the step service.
40
     */
41
    public function initializeMiddleware()
42
    {
43
        $this->service = StepMiddlewareService::get();
44
    }
45
46
    /**
47
     * @param Arguments $arguments
48
     */
49
    public function after(Arguments $arguments)
50
    {
51
        if ($this->getFormObject()->formWasSubmitted()) {
52
            return;
53
        }
54
55
        $formObject = $this->getFormObject();
56
57
        if (false === $formObject->getDefinition()->hasSteps()) {
58
            return;
59
        }
60
61
        $this->currentStep = $this->getCurrentStep();
62
63
        if (null === $this->currentStep
64
            || false === $this->currentStep->hasSubsteps()
65
        ) {
66
            return;
67
        }
68
69
        $this->stepService = FormObjectFactory::get()->getStepService($formObject);
70
71
        $this->fetchCurrentSubstep();
72
    }
73
74
    /**
75
     * Fetches the current substep that should be displayed (the first
76
     * substep(s) may have an activation condition).
77
     */
78
    private function fetchCurrentSubstep()
79
    {
80
        $this->service->reset($this->getFormObject(), $this->getRequest());
81
82
        $substepDefinition = $this->service->findFirstSubstepDefinition($this->currentStep);
83
84
        $this->stepService->setCurrentSubstepDefinition($substepDefinition);
0 ignored issues
show
Bug introduced by
It seems like $substepDefinition defined by $this->service->findFirs...ion($this->currentStep) on line 82 can be null; however, Romm\Formz\Form\FormObje...rentSubstepDefinition() 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...
85
    }
86
}
87