Completed
Push — wip/steps ( a89def...4fedf8 )
by Romain
02:24
created

StepDispatchingMiddleware   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 0
loc 73
rs 10
c 3
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeMiddleware() 0 4 1
D process() 0 41 9
A getAllowedSignals() 0 4 1
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\Item\DefaultMiddleware;
18
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
19
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
20
use Romm\Formz\Middleware\Processor\RemoveFromSingleFieldValidationContext;
21
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
/**
25
 * This middleware should be the last one called, as it is used to dispatch the
26
 * request to the next step, if there is one.
27
 */
28
class StepDispatchingMiddleware extends DefaultMiddleware implements PresetMiddlewareInterface, SendsMiddlewareSignal, RemoveFromSingleFieldValidationContext
29
{
30
    /**
31
     * @var int
32
     */
33
    protected $priority = self::PRIORITY_STEP;
34
35
    /**
36
     * @var StepMiddlewareService
37
     */
38
    protected $service;
39
40
    /**
41
     * Inject the step service.
42
     */
43
    public function initializeMiddleware()
44
    {
45
        $this->service = StepMiddlewareService::get();
46
    }
47
48
    /**
49
     * @see StepDispatchingMiddleware
50
     */
51
    protected function process()
52
    {
53
        $formObject = $this->getFormObject();
54
55
        if (false === $formObject->getDefinition()->hasSteps()) {
56
            return;
57
        }
58
59
        $formResult = $formObject->getFormResult();
60
61
        if ($formObject->formWasSubmitted()
62
            && false === $formResult->hasErrors()
63
        ) {
64
            $currentStep = $this->getCurrentStep();
65
66
            if ($currentStep) {
67
                $stepService = FormObjectFactory::get()->getStepService($formObject);
68
69
                if ($currentStep->hasSubsteps()
70
                    && false === $stepService->lastSubstepWasValidated()
71
                ) {
72
                    return;
73
                }
74
75
                $nextStep = $this->service->getNextStep($currentStep);
76
77
                if ($nextStep) {
78
                    /** @var StepDispatchingArguments $arguments */
79
                    $arguments = GeneralUtility::makeInstance(StepDispatchingArguments::class);
80
81
                    $this->beforeSignal()
82
                        ->withArguments($arguments)
83
                        ->dispatch();
84
85
                    if (false === $arguments->getCancelStepDispatching()) {
86
                        $this->service->moveForwardToStep($nextStep, $this->redirect());
87
                    }
88
                }
89
            }
90
        }
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function getAllowedSignals()
97
    {
98
        return [StepDispatchingSignal::class];
99
    }
100
}
101