Completed
Push — middleware-wip ( ab9573...668c38 )
by Romain
10:14
created

PersistenceFetchingMiddleware::before()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 12
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\Domain\Model\FormMetadata;
17
use Romm\Formz\Domain\Repository\FormMetadataRepository;
18
use Romm\Formz\Form\FormObject\FormObjectFactory;
19
use Romm\Formz\Middleware\Argument\Arguments;
20
use Romm\Formz\Middleware\Item\AbstractMiddleware;
21
use Romm\Formz\Middleware\Item\FormInjection\FormInjectionSignal;
22
use Romm\Formz\Middleware\Signal\Before;
23
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal;
24
use Romm\Formz\Middleware\State\PresetMiddlewareInterface;
25
26
/**
27
 * This middleware will try to fetch a form instance using the persistence
28
 * manager.
29
 *
30
 * If a form identifier hash is found in the request arguments, it is used to
31
 * search for a form instance in every persistence service bound to the form
32
 * object.
33
 */
34
class PersistenceFetchingMiddleware extends AbstractMiddleware implements Before, FormInjectionSignal, SendsMiddlewareSignal, PresetMiddlewareInterface
35
{
36
    /**
37
     * @var \Romm\Formz\Middleware\Item\Persistence\PersistenceFetchingMiddlewareOption
38
     */
39
    protected $options;
40
41
    /**
42
     * @var FormMetadata
43
     */
44
    protected $metadata;
45
46
    /**
47
     * @var FormMetadataRepository
48
     */
49
    protected $metadataRepository;
50
51
    /**
52
     * Checks for a form identifier hash in the request arguments, and tries to
53
     * get a form identifier object with it.
54
     */
55
    public function initializeMiddleware()
56
    {
57
        $hash = $this->getFormHash();
58
59
        if ($hash) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $hash of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
60
            $this->metadata = $this->metadataRepository->findOneByHash($hash);
61
62
            if ($this->metadata
63
                && $this->metadata->getClassName() !== $this->getFormObject()->getClassName()
64
            ) {
65
                $this->metadata = null;
66
            }
67
68
            if (!$this->metadata) {
69
                // @todo forward
70
                $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...
71
            }
72
        }
73
    }
74
75
    /**
76
     * @see PersistenceFetchingMiddleware
77
     *
78
     * @param Arguments $arguments
79
     */
80
    public function before(Arguments $arguments)
81
    {
82
        if ($this->metadata
83
            && false === $this->getFormObject()->formWasSubmitted()
84
        ) {
85
            $this->beforeSignal()->dispatch();
86
87
            $formObject = $this->getFormObject();
88
            $persistenceManager = $formObject->getPersistenceManager();
89
90
            $form = $persistenceManager->fetchFirst($this->metadata);
91
92
            if ($form) {
93
                $formObject->setForm($form);
94
95
                $proxy = FormObjectFactory::get()->getProxy($formObject, $form);
96
                $proxy->setFormHash($this->metadata->getHash());
97
98
                $this->afterSignal()->dispatch();
99
            }
100
        }
101
    }
102
103
    /**
104
     * This function tries to fetch the form identifier hash for the current
105
     * form, in the request arguments.
106
     *
107
     * @return string|null
108
     */
109
    protected function getFormHash()
110
    {
111
        $formObject = $this->getFormObject();
112
113
        if ($this->getRequest()->hasArgument('fz-hash')) {
114
            $formName = $formObject->getName();
115
            $identifierList = $this->getRequest()->getArgument('fz-hash');
116
117
            if (is_array($identifierList)
118
                && isset($identifierList[$formName])
119
            ) {
120
                return (string)$identifierList[$formName];
121
            }
122
        }
123
124
        return $formObject->hasForm()
125
            ? $formObject->getFormHash()
126
            : null;
127
    }
128
129
    /**
130
     * @param FormMetadataRepository $metadataRepository
131
     */
132
    public function injectMetadataRepository(FormMetadataRepository $metadataRepository)
133
    {
134
        $this->metadataRepository = $metadataRepository;
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public function getAllowedSignals()
141
    {
142
        return [PersistenceFetchingSignal::class];
143
    }
144
}
145