Completed
Push — wip/steps ( 7c3be8...c6080c )
by Romain
16:53
created

PreviousStepMiddleware   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 10
dl 0
loc 117
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeMiddleware() 0 4 1
A after() 0 20 4
C redirectToPreviousStep() 0 36 7
B handleRedirectionSubstep() 0 18 5
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\FormObject\FormObjectFactory;
18
use Romm\Formz\Form\FormObject\Service\FormObjectSteps;
19
use Romm\Formz\Middleware\Argument\Arguments;
20
use Romm\Formz\Middleware\Item\AbstractMiddleware;
21
use Romm\Formz\Middleware\Item\FormInjection\FormInjectionSignal;
22
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
23
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
24
use Romm\Formz\Middleware\Scope\FieldValidationScope;
25
use Romm\Formz\Middleware\Scope\ReadScope;
26
use Romm\Formz\Middleware\Signal\After;
27
use Romm\Formz\ViewHelpers\Step\PreviousLinkViewHelper;
28
29
/**
30
 * @todo
31
 */
32
class PreviousStepMiddleware extends AbstractMiddleware implements After, FormInjectionSignal, PresetMiddlewareInterface
33
{
34
    /**
35
     * @var int
36
     */
37
    protected $priority = self::PRIORITY_STEP_FETCHING + 100;
38
39
    /**
40
     * @var Step
41
     */
42
    protected $currentStep;
43
44
    /**
45
     * @var FormObjectSteps
46
     */
47
    protected $stepService;
48
49
    /**
50
     * @var StepMiddlewareService
51
     */
52
    protected $service;
53
54
    /**
55
     * @var array
56
     */
57
    protected static $defaultScopesBlackList = [ReadScope::class, FieldValidationScope::class];
58
59
    /**
60
     * Inject the step service.
61
     */
62
    public function initializeMiddleware()
63
    {
64
        $this->service = StepMiddlewareService::get();
65
    }
66
67
    /**
68
     * @see PreviousStepMiddleware
69
     *
70
     * @param Arguments $arguments
71
     */
72
    public function after(Arguments $arguments)
73
    {
74
        $formObject = $this->getFormObject();
75
        $this->service->reset($formObject, $this->getRequest());
76
77
        if (false === $formObject->getDefinition()->hasSteps()) {
78
            return;
79
        }
80
81
        $this->currentStep = $this->getCurrentStep();
82
        $this->stepService = FormObjectFactory::get()->getStepService($this->getFormObject());
83
84
        if ($this->currentStep) {
85
            if ($this->getRequest()->hasArgument(PreviousLinkViewHelper::PREVIOUS_LINK_PARAMETER)) {
86
                $this->redirectToPreviousStep();
87
            } else {
88
                $this->handleRedirectionSubstep();
89
            }
90
        }
91
    }
92
93
    protected function redirectToPreviousStep()
94
    {
95
        $stepDefinition = $this->service->getStepDefinition($this->currentStep);
96
97
        if ($this->currentStep->hasSubsteps()) {
98
            $substepsLevel = $this->stepService->getSubstepsLevel();
99
100
            if ($substepsLevel > 1) {
101
                $substepDefinition = $this->currentStep->getSubsteps()->getFirstSubstepDefinition();
102
103
                while ($substepDefinition) {
104
                    $nextSubstepDefinition = $this->service->getNextSubstepDefinition($substepDefinition);
105
106
                    if (!$nextSubstepDefinition
107
                        || $nextSubstepDefinition->getLevel() >= $substepsLevel
108
                    ) {
109
                        break;
110
                    }
111
112
                    $substepDefinition = $nextSubstepDefinition;
113
                }
114
115
                $this->stepService->setCurrentSubstepDefinition($substepDefinition);
116
                $this->stepService->setSubstepsLevel($substepDefinition->getLevel());
117
118
                return;
119
            }
120
        }
121
122
        if ($stepDefinition->hasPreviousDefinition()) {
123
            $this->service->redirectToStep(
124
                $stepDefinition->getPreviousDefinition()->getStep(),
125
                $this->redirect()->withArguments(['fz-last-substep' => true])
126
            );
127
        }
128
    }
129
130
    protected function handleRedirectionSubstep()
131
    {
132
        if ($this->currentStep->hasSubsteps()
133
            && $this->getRequest()->hasArgument('fz-last-substep')
134
        ) {
135
            $substepDefinition = $this->currentStep->getSubsteps()->getFirstSubstepDefinition();
136
137
            do {
138
                $nextSubstepDefinition = $this->service->getNextSubstepDefinition($substepDefinition);
139
140
                if ($nextSubstepDefinition) {
141
                    $substepDefinition = $nextSubstepDefinition;
142
                }
143
            } while ($nextSubstepDefinition);
144
145
            $this->stepService->setCurrentSubstepDefinition($substepDefinition);
146
        }
147
    }
148
}
149