Completed
Push — wip/steps ( 47b00f...fe9b9d )
by Romain
02:49
created

StepDispatchingMiddleware::process()   C

Complexity

Conditions 9
Paths 7

Size

Total Lines 47
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 47
rs 5.2941
c 3
b 0
f 0
cc 9
eloc 22
nc 7
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\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
                /*
68
                 * No error during the validation : the submitted form values
69
                 * are saved in the step metadata.
70
                 */
71
                $this->service->saveStepFormValues($currentStep);
72
73
                $stepService = FormObjectFactory::get()->getStepService($formObject);
74
75
                if ($currentStep->hasSubsteps()
76
                    && false === $stepService->lastSubstepWasValidated()
77
                ) {
78
                    return;
79
                }
80
81
                $nextStep = $this->service->getNextStep($currentStep);
82
83
                if ($nextStep) {
84
                    /** @var StepDispatchingArguments $arguments */
85
                    $arguments = GeneralUtility::makeInstance(StepDispatchingArguments::class);
86
87
                    $this->beforeSignal()
88
                        ->withArguments($arguments)
89
                        ->dispatch();
90
91
                    if (false === $arguments->getCancelStepDispatching()) {
92
                        $this->service->moveForwardToStep($nextStep, $this->redirect());
93
                    }
94
                }
95
            }
96
        }
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function getAllowedSignals()
103
    {
104
        return [StepDispatchingSignal::class];
105
    }
106
}
107