Completed
Push — middleware-wip-tmp2 ( 51115e...a3478c )
by Romain
02:39
created

FormObjectSteps   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 93
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getCurrentStep() 0 35 6
A amongSteps() 0 9 1
A amongStepsRecursive() 0 10 3
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\Form\FormObject\Service;
15
16
use Romm\Formz\Configuration\Form\Step\Step\Step;
17
use Romm\Formz\Core\Core;
18
use Romm\Formz\Form\FormObject\FormObject;
19
use TYPO3\CMS\Extbase\Mvc\Web\Request;
20
21
class FormObjectSteps
22
{
23
    /**
24
     * @var FormObject
25
     */
26
    protected $formObject;
27
28
    /**
29
     * @param FormObject $formObject
30
     */
31
    public function __construct(FormObject $formObject)
32
    {
33
        $this->formObject = $formObject;
34
    }
35
36
    /**
37
     * This function will search among the registered steps to find the one that
38
     * has the same controller parameters.
39
     *
40
     * It is also possible not to find any step, in this case `null` is
41
     * returned.
42
     *
43
     * @param Request $request
44
     * @return Step|null
45
     */
46
    public function getCurrentStep(Request $request)
47
    {
48
        $configuration = $this->formObject->getConfiguration();
49
50
        if (false === $configuration->hasSteps()) {
51
            return null;
52
        }
53
54
        $currentStep = null;
55
56
        $this->amongSteps(function (Step $step) use ($request, &$currentStep) {
57
            $data = [
58
                $step->getPageUid()    => Core::get()->getPageController()->id,
59
                $step->getExtension()  => $request->getControllerExtensionName(),
60
                $step->getController() => $request->getControllerName(),
61
                $step->getAction()     => $request->getControllerActionName(),
62
            ];
63
64
            foreach ($data as $stepData => $requestData) {
65
                if (false === empty($stepData)
66
                    && $stepData !== $requestData
67
                ) {
68
                    return;
69
                }
70
            }
71
72
            if (null !== $currentStep) {
73
                throw new \Exception('todo'); // @todo
74
            }
75
76
            $currentStep = $step;
77
        });
78
79
        return $currentStep;
80
    }
81
82
    /**
83
     * @todo
84
     *
85
     * @param callable $callback
86
     */
87
    protected function amongSteps(callable $callback)
88
    {
89
        $firstStep = $this->formObject
90
            ->getConfiguration()
91
            ->getSteps()
92
            ->getFirstStep();
93
94
        $this->amongStepsRecursive($firstStep, $callback);
95
    }
96
97
    /**
98
     * @todo
99
     *
100
     * @param Step     $step
101
     * @param callable $callback
102
     */
103
    protected function amongStepsRecursive(Step $step, callable $callback)
104
    {
105
        call_user_func($callback, $step);
106
107
        if ($step->hasNextStep()) {
108
            foreach ($step->getNextStep()->getAllSteps() as $step) {
109
                $this->amongStepsRecursive($step, $callback);
110
            }
111
        }
112
    }
113
}
114