Completed
Push — middleware-wip-tmp ( d8f2e1 )
by Romain
02:50
created

getFormIdentifierHash()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 8
nc 3
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
        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($hash, '$hash');
57
58
        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...
59
            $this->metadata = $this->metadataRepository->findOneByHash($hash);
60
            \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->metadata, '$this->metadata');
61
62
            if (!$this->metadata) {
63
                // @todo forward
64
                $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...
65
            }
66
        }
67
    }
68
69
    /**
70
     * @see PersistenceFetchingMiddleware
71
     *
72
     * @param Arguments $arguments
73
     */
74
    public function before(Arguments $arguments)
75
    {
76
        if ($this->metadata
77
            && false === $this->getFormObject()->formWasSubmitted()
78
        ) {
79
            $formObject = $this->getFormObject();
80
            $form = $formObject->getPersistenceManager()->fetchFirst();
81
82
            if ($form) {
83
                $formObject->setForm($form);
84
                $formObject->setFormHash($this->metadata->getHash());
85
            }
86
        }
87
    }
88
89
    /**
90
     * This function tries to fetch the form identifier hash for the current
91
     * form, in the request arguments.
92
     *
93
     * @return string|null
94
     */
95
    protected function getFormHash()
96
    {
97
        if ($this->getRequest()->hasArgument('fz-hash')) {
98
            $formName = $this->getFormObject()->getName();
99
            \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($formName, '$formName');
100
            $identifierList = $this->getRequest()->getArgument('fz-hash');
101
102
            if (is_array($identifierList)
103
                && isset($identifierList[$formName])
104
            ) {
105
                return (string)$identifierList[$formName];
106
            }
107
        }
108
109
        return null;
110
    }
111
112
    /**
113
     * @param FormMetadataRepository $metadataRepository
114
     */
115
    public function injectMetadataRepository(FormMetadataRepository $metadataRepository)
116
    {
117
        $this->metadataRepository = $metadataRepository;
118
    }
119
}
120