Completed
Push — middleware-wip ( 1bcdac...ffd957 )
by Romain
02:46
created

initializeMiddleware()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 5
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\Persistence;
15
16
use Romm\Formz\Domain\Model\FormMetadata;
17
use Romm\Formz\Domain\Repository\FormMetadataRepository;
18
use Romm\Formz\Middleware\Argument\Arguments;
19
use Romm\Formz\Middleware\Item\AbstractMiddleware;
20
use Romm\Formz\Middleware\Item\FormInjection\FormInjectionSignal;
21
use Romm\Formz\Middleware\Signal\Before;
22
use Romm\Formz\Middleware\State\PresetMiddlewareInterface;
23
24
/**
25
 * This middleware will try to fetch a form instance using the persistence
26
 * manager.
27
 *
28
 * If a form identifier hash is found in the request arguments, it is used to
29
 * search for a form instance in every persistence service bound to the form
30
 * object.
31
 */
32
class PersistenceFetchingMiddleware extends AbstractMiddleware implements Before, FormInjectionSignal, PresetMiddlewareInterface
33
{
34
    /**
35
     * @var \Romm\Formz\Middleware\Item\Persistence\PersistenceFetchingMiddlewareOption
36
     */
37
    protected $options;
38
39
    /**
40
     * @var FormMetadata
41
     */
42
    protected $metadata;
43
44
    /**
45
     * @var FormMetadataRepository
46
     */
47
    protected $metadataRepository;
48
49
    /**
50
     * Checks for a form identifier hash in the request arguments, and tries to
51
     * get a form identifier object with it.
52
     */
53
    public function initializeMiddleware()
54
    {
55
        $hash = $this->getFormHash();
56
57
        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...
58
            $this->metadata = $this->metadataRepository->findOneByHash($hash);
59
60
            if ($this->metadata
61
                && $this->metadata->getClassName() !== $this->getFormObject()->getClassName()
62
            ) {
63
                $this->metadata = null;
64
            }
65
66
            if (!$this->metadata) {
67
                // @todo forward
68
                $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...
69
            }
70
        }
71
    }
72
73
    /**
74
     * @see PersistenceFetchingMiddleware
75
     *
76
     * @param Arguments $arguments
77
     */
78
    public function before(Arguments $arguments)
79
    {
80
        if ($this->metadata
81
            && false === $this->getFormObject()->formWasSubmitted()
82
        ) {
83
            $formObject = $this->getFormObject();
84
            $form = $formObject->getPersistenceManager()->fetchFirst($this->metadata);
85
86
            if ($form) {
87
                $formObject->setForm($form);
88
                $formObject->setMetadata($this->metadata);
89
            }
90
        }
91
    }
92
93
    /**
94
     * This function tries to fetch the form identifier hash for the current
95
     * form, in the request arguments.
96
     *
97
     * @return string|null
98
     */
99
    protected function getFormHash()
100
    {
101
        if ($this->getRequest()->hasArgument('fz-hash')) {
102
            $formName = $this->getFormObject()->getName();
103
            $identifierList = $this->getRequest()->getArgument('fz-hash');
104
105
            if (is_array($identifierList)
106
                && isset($identifierList[$formName])
107
            ) {
108
                return (string)$identifierList[$formName];
109
            }
110
        }
111
112
        return null;
113
    }
114
115
    /**
116
     * @param FormMetadataRepository $metadataRepository
117
     */
118
    public function injectMetadataRepository(FormMetadataRepository $metadataRepository)
119
    {
120
        $this->metadataRepository = $metadataRepository;
121
    }
122
}
123