Completed
Push — wip/steps-tmp ( ae03e8...c2561f )
by Romain
02:16
created

PreviousStepMiddleware::after()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 56
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 6.7741
c 0
b 0
f 0
cc 10
eloc 27
nc 10
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\FormObject\FormObjectFactory;
17
use Romm\Formz\Middleware\Argument\Arguments;
18
use Romm\Formz\Middleware\Item\AbstractMiddleware;
19
use Romm\Formz\Middleware\Item\FormInjection\FormInjectionSignal;
20
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
21
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
22
use Romm\Formz\Middleware\Scope\FieldValidationScope;
23
use Romm\Formz\Middleware\Scope\ReadScope;
24
use Romm\Formz\Middleware\Signal\After;
25
use Romm\Formz\ViewHelpers\Step\PreviousLinkViewHelper;
26
27
/**
28
 * @todo
29
 */
30
class PreviousStepMiddleware extends AbstractMiddleware implements After, FormInjectionSignal, PresetMiddlewareInterface
31
{
32
    /**
33
     * @var int
34
     */
35
    protected $priority = self::PRIORITY_STEP_FETCHING + 100;
36
37
    /**
38
     * @var StepMiddlewareService
39
     */
40
    protected $service;
41
42
    /**
43
     * @var array
44
     */
45
    protected static $defaultScopesBlackList = [ReadScope::class, FieldValidationScope::class];
46
47
    /**
48
     * Inject the step service.
49
     */
50
    public function initializeMiddleware()
51
    {
52
        $this->service = StepMiddlewareService::get();
53
    }
54
55
    /**
56
     * @see PreviousStepMiddleware
57
     *
58
     * @param Arguments $arguments
59
     */
60
    public function after(Arguments $arguments)
61
    {
62
        $formObject = $this->getFormObject();
63
        $this->service->reset($formObject, $this->getRequest());
64
65
        if (false === $formObject->getDefinition()->hasSteps()) {
66
            return;
67
        }
68
69
        if (!$this->getRequest()->hasArgument(PreviousLinkViewHelper::PREVIOUS_LINK_PARAMETER)) {
70
            return;
71
        }
72
73
        $currentStep = $this->getCurrentStep();
74
75
        if ($currentStep) {
76
            $stepDefinition = $this->service->getStepDefinition($currentStep);
77
78
            if ($currentStep->hasSubsteps()) {
79
                $stepService = FormObjectFactory::get()->getStepService($this->getFormObject());
80
                $substepsLevel = $stepService->getSubstepsLevel();
81
82
                if ($substepsLevel > 1) {
83
                    $substepDefinition = $currentStep->getSubsteps()->getFirstSubstepDefinition();
84
85
                    while ($substepDefinition) {
86
                        $nextSubstepDefinition = $this->service->getNextSubstepDefinition($substepDefinition);
87
88
                        if (!$nextSubstepDefinition
89
                            || $nextSubstepDefinition->getLevel() >= $substepsLevel - 1
90
                        ) {
91
                            break;
92
                        }
93
94
                        $substepDefinition = $nextSubstepDefinition;
95
                    }
96
97
                    $stepService->setCurrentSubstepDefinition($substepDefinition);
98
                    $stepService->setCurrentStep($currentStep);
99
                    $stepService->setSubstepsLevel($substepDefinition->getLevel());
100
//                    \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($substepDefinition, __CLASS__ . ':' . __LINE__ . ' $substepDefinition');
101
//                    \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($substepDefinition->getLevel(), __CLASS__ . ':' . __LINE__ . ' $substepDefinition->getLevel()');
102
//                    \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($currentStep, __CLASS__ . ':' . __LINE__ . ' $currentStep');
103
//                    \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($stepService, __CLASS__ . ':' . __LINE__ . ' $stepService');
104
//                    die();
105
106
                    return;
107
                }
108
            }
109
110
111
            if ($stepDefinition->hasPreviousDefinition()) {
112
                $this->service->redirectToStep($stepDefinition->getPreviousDefinition()->getStep(), $this->redirect());
113
            }
114
        }
115
    }
116
}
117