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

StepFetchingMiddleware::before()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 11
nc 4
nop 1
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\Configuration\Form\Step\Step\Step;
17
use Romm\Formz\Middleware\Argument\Arguments;
18
use Romm\Formz\Middleware\Item\AbstractMiddleware;
19
use Romm\Formz\Middleware\Item\FormValidation\FormValidationSignal;
20
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
21
use Romm\Formz\Middleware\Signal\Before;
22
23
/**
24
 * @todo
25
 */
26
class StepFetchingMiddleware extends AbstractMiddleware implements Before, FormValidationSignal
27
{
28
    /**
29
     * @var int
30
     */
31
    protected $priority = self::PRIORITY_STEP;
32
33
    /**
34
     * @var StepMiddlewareService
35
     */
36
    protected $service;
37
38
    /**
39
     * @todo
40
     */
41
    public function initializeMiddleware()
42
    {
43
        $this->service = StepMiddlewareService::get();
44
        $this->service->reset($this->getFormObject());
45
    }
46
47
    /**
48
     * @param Arguments $arguments
49
     */
50
    public function before(Arguments $arguments)
51
    {
52
        $formObject = $this->getFormObject();
53
54
        if (false === $formObject->getConfiguration()->hasSteps()) {
55
            return;
56
        }
57
58
        $currentStep = $this->getCurrentStep();
59
        
60
61
        if ($currentStep) {
62
            $stepToRedirect = $this->service->validateStep($currentStep);
63
            \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($currentStep, '$currentStep');
64
65
            if ($stepToRedirect instanceof Step) {
66
                die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method before() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
67
                $this->redirectToStep($stepToRedirect);
0 ignored issues
show
Unused Code introduced by
$this->redirectToStep($stepToRedirect); 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...
68
            }
69
        }
70
    }
71
72
    protected function redirectToStep(Step $step)
73
    {
74
        $formObject = $this->getFormObject();
75
76
        $this->redirect()
77
            ->toPage($step->getPageUid())
78
            ->toExtension($step->getExtension())
79
            ->toController($step->getController())
80
            ->toAction($step->getAction())
81
            ->withArguments([
82
                'fz-hash' => [
83
                    $formObject->getName() => $formObject->getFormHash()
84
                ]
85
            ])
86
            ->dispatch();
87
    }
88
}
89