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

FieldFocusMiddleware::initializeMiddleware()   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\Field\Focus;
15
16
use Romm\Formz\Form\Definition\Field\Field;
17
use Romm\Formz\Form\Definition\Step\Step\Step;
18
use Romm\Formz\Form\Definition\Step\Step\Substep\SubstepDefinition;
19
use Romm\Formz\Form\FormObject\FormObjectFactory;
20
use Romm\Formz\Middleware\Item\OnBeginMiddleware;
21
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
22
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
23
24
/**
25
 * @todo
26
 */
27
class FieldFocusMiddleware extends OnBeginMiddleware implements PresetMiddlewareInterface
28
{
29
    const FIELD_FOCUS_ARGUMENT = 'fieldFocus';
30
31
    /**
32
     * @var Step
33
     */
34
    protected $step;
35
36
    /**
37
     * @var Field
38
     */
39
    protected $field;
40
41
    /**
42
     * @var StepMiddlewareService
43
     */
44
    protected $service;
45
46
    /**
47
     * Inject the step service.
48
     */
49
    public function initializeMiddleware()
50
    {
51
        $this->service = StepMiddlewareService::get();
52
    }
53
54
    /**
55
     * @todo
56
     */
57
    protected function process()
58
    {
59
        if ($this->getFormObject()->formWasSubmitted()) {
60
            return;
61
        }
62
63
        $this->step = $this->getCurrentStep();
64
65
        if (!$this->step) {
66
            return;
67
        }
68
69
        if (false === $this->step->hasSubsteps()) {
70
            return;
71
        }
72
73
        $this->fetchField();
74
75
        if (null === $this->field) {
76
            return;
77
        }
78
79
        $substepDefinition = $this->fetchFieldSubstep();
80
81
        if ($substepDefinition) {
82
            $stepService = FormObjectFactory::get()->getStepService($this->getFormObject());
83
            $stepService->setCurrentSubstepDefinition($substepDefinition);
84
        }
85
    }
86
87
    /**
88
     * @return SubstepDefinition|null
89
     */
90
    protected function fetchFieldSubstep()
91
    {
92
        return $this->service->findSubstepDefinition(
93
            $this->step,
94
            function (SubstepDefinition $substepDefinition) {
95
                return $substepDefinition->getSubstep()->supportsField($this->field);
96
            }
97
        );
98
    }
99
100
    protected function fetchField()
101
    {
102
        $request = $this->getRequest();
103
104
        if ($request->hasArgument(self::FIELD_FOCUS_ARGUMENT)) {
105
            $fieldName = $request->getArgument(self::FIELD_FOCUS_ARGUMENT);
106
            $formDefinition = $this->getFormObject()->getDefinition();
107
108
            if ($formDefinition->hasField($fieldName)) {
109
                $field = $formDefinition->getField($fieldName);
110
111
                if ($this->step->supportsField($field)) {
112
                    $this->field = $field;
113
                }
114
            }
115
        }
116
    }
117
}
118