Completed
Push — wip/steps ( 06abc1...278315 )
by Romain
02:29
created

StepDispatchingMiddleware::getAllowedSignals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
23
/**
24
 * This middleware should be the last one called, as it is used to dispatch the
25
 * request to the next step, if there is one.
26
 */
27
class StepDispatchingMiddleware extends DefaultMiddleware implements PresetMiddlewareInterface, SendsMiddlewareSignal, RemoveFromSingleFieldValidationContext
28
{
29
    /**
30
     * @var int
31
     */
32
    protected $priority = self::PRIORITY_STEP;
33
34
    /**
35
     * @var StepMiddlewareService
36
     */
37
    protected $service;
38
39
    /**
40
     * Inject the step service.
41
     */
42
    public function initializeMiddleware()
43
    {
44
        $this->service = StepMiddlewareService::get();
45
    }
46
47
    /**
48
     * @see StepDispatchingMiddleware
49
     */
50
    protected function process()
51
    {
52
        $formObject = $this->getFormObject();
53
54
        if (false === $formObject->getDefinition()->hasSteps()) {
55
            return;
56
        }
57
58
        $formResult = $formObject->getFormResult();
59
60
        if ($formObject->formWasSubmitted()
61
            && false === $formResult->hasErrors()
62
        ) {
63
            $currentStep = $this->getCurrentStep();
64
65
            if ($currentStep) {
66
                $stepService = FormObjectFactory::get()->getStepService($formObject);
67
68
                if ($currentStep->hasSubsteps()
69
                    && false === $stepService->lastSubstepWasValidated()
70
                ) {
71
                    return;
72
                }
73
74
                $nextStep = $this->service->getNextStep($currentStep);
75
76
                if ($nextStep) {
77
                    $this->beforeSignal()->dispatch();
78
79
                    $this->service->moveForwardToStep($nextStep, $this->redirect());
80
                }
81
            }
82
        }
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public function getAllowedSignals()
89
    {
90
        return [StepDispatchingSignal::class];
91
    }
92
}
93