Completed
Push — wip/steps ( b2c91c...6fd1d1 )
by Romain
02:28
created

FieldFocusMiddleware::process()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
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\Substep;
19
use Romm\Formz\Form\Definition\Step\Step\Substep\SubstepDefinition;
20
use Romm\Formz\Form\FormObject\FormObjectFactory;
21
use Romm\Formz\Middleware\Item\OnBeginMiddleware;
22
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
23
24
/**
25
 * @todo
26
 */
27
class FieldFocusMiddleware extends OnBeginMiddleware implements PresetMiddlewareInterface
28
{
29
    /**
30
     * @var Step
31
     */
32
    protected $step;
33
34
    /**
35
     * @var Field
36
     */
37
    protected $field;
38
39
    /**
40
     * @todo
41
     */
42
    protected function process()
43
    {
44
        return;
45
46
        if ($this->getFormObject()->formWasSubmitted()) {
0 ignored issues
show
Unused Code introduced by
if ($this->getFormObject...tted()) { return; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
47
            return;
48
        }
49
50
        $this->step = $this->getCurrentStep();
51
52
        if (!$this->step) {
53
            return;
54
        }
55
56
        if (false === $this->step->hasSubsteps()) {
57
            return;
58
        }
59
60
        $this->fetchField();
61
62
        if (null === $this->field) {
63
            return;
64
        }
65
66
        $firstSubstepDefinition = $this->step->getSubsteps()->getFirstSubstepDefinition();
67
        $substepDefinition = $this->fetchFieldSubstep($firstSubstepDefinition);
68
69
        $stepService = FormObjectFactory::get()->getStepService($this->getFormObject());
70
        $stepService->setCurrentSubstepDefinition($substepDefinition);
71
    }
72
73
    /**
74
     * @param SubstepDefinition $substepDefinition
75
     * @return SubstepDefinition|null
76
     */
77
    protected function fetchFieldSubstep(SubstepDefinition $substepDefinition)
78
    {
79
        if ($substepDefinition->getSubstep()->supportsField($this->field)) {
80
            return $substepDefinition;
81
        }
82
83
        if ($substepDefinition->hasNextSubstep()) {
84
            return $this->fetchFieldSubstep($substepDefinition->getNextSubstep());
85
        }
86
87
        return null;
88
    }
89
90
    protected function fetchField()
91
    {
92
        $fieldName = 'nom';
93
        $formDefinition = $this->getFormObject()->getDefinition();
94
95
        if ($formDefinition->hasField($fieldName)) {
96
            $field = $formDefinition->getField($fieldName);
97
98
            if ($this->step->supportsField($field)) {
99
                $this->field = $field;
100
            }
101
        }
102
    }
103
}
104