Completed
Push — middleware-wip ( 71b03c...531d78 )
by Romain
03:32
created

PersistenceFetchingMiddleware::before()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 8
nc 3
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\Persistence;
15
16
use Romm\Formz\Form\Service\DataObject\WrongFormIdentifierObjectDataException;
17
use Romm\Formz\Middleware\Argument\Arguments;
18
use Romm\Formz\Middleware\Item\AbstractMiddleware;
19
use Romm\Formz\Middleware\Item\FormInjection\FormInjectionSignal;
20
use Romm\Formz\Middleware\Signal\Before;
21
use Romm\Formz\Middleware\State\PresetMiddlewareInterface;
22
23
/**
24
 * This middleware will try to fetch a form instance using the persistence
25
 * manager.
26
 *
27
 * If a form identifier hash is found in the request arguments, it is used to
28
 * search for a form instance in every persistence service bound to the form
29
 * object.
30
 */
31
class PersistenceFetchingMiddleware extends AbstractMiddleware implements Before, FormInjectionSignal, PresetMiddlewareInterface
32
{
33
    /**
34
     * @var \Romm\Formz\Middleware\Item\Persistence\PersistenceFetchingMiddlewareOption
35
     */
36
    protected $options;
37
38
    /**
39
     * @var PersistenceMiddlewareService
40
     */
41
    protected $service;
42
43
    /**
44
     * Checks for a form identifier hash in the request arguments, and tries to
45
     * get a form identifier object with it.
46
     */
47
    public function initializeMiddleware()
48
    {
49
        if ($this->getRequest()->hasArgument('fz-identifier')) {
50
            $identifier = $this->getRequest()->getArgument('fz-identifier');
51
52
            try {
53
                $identifierObject = $this->getFormObject()->getFormIdentifierObject($identifier);
54
55
                $this->service->setIdentifierObject($identifierObject);
56
            } catch (WrongFormIdentifierObjectDataException $exception) {
57
                // @todo forward
58
                $action = $this->options->getForwardToActionOnHashError();
0 ignored issues
show
Unused Code introduced by
$action is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
            }
60
        }
61
    }
62
63
    /**
64
     * @see PersistenceFetchingMiddleware
65
     *
66
     * @param Arguments $arguments
67
     */
68
    public function before(Arguments $arguments)
69
    {
70
        if (false === $this->getFormObject()->formWasSubmitted()
71
            && $this->service->hasIdentifierObject()
72
        ) {
73
            $form = $this->getFormObject()
74
                ->getPersistenceManager()
75
                ->fetchFirst($this->service->getIdentifierObject());
76
77
            if ($form) {
78
                $this->getFormObject()->setForm($form);
79
            }
80
        }
81
    }
82
83
    /**
84
     * @param PersistenceMiddlewareService $service
85
     */
86
    public function injectService(PersistenceMiddlewareService $service)
87
    {
88
        $this->service = $service;
89
        $this->service->reset();
90
    }
91
}
92