Completed
Push — wip/steps ( 107e99...47b00f )
by Romain
02:27
created

FormService::getFormIdentifier()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
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\Begin\Service;
15
16
use Romm\Formz\Domain\Repository\FormMetadataRepository;
17
use Romm\Formz\Form\FormInterface;
18
use Romm\Formz\Middleware\Processor\MiddlewareProcessor;
19
use TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter;
20
21
class FormService
22
{
23
    /**
24
     * @var MiddlewareProcessor
25
     */
26
    private $processor;
27
28
    /**
29
     * @var FormMetadataRepository
30
     */
31
    protected $formMetadataRepository;
32
33
    /**
34
     * @param MiddlewareProcessor $processor
35
     */
36
    public function __construct(MiddlewareProcessor $processor)
37
    {
38
        $this->processor = $processor;
39
    }
40
41
    /**
42
     * @return FormInterface
43
     */
44
    public function getFormInstance()
45
    {
46
        $formName = $this->processor->getFormObject()->getName();
47
        $argument = $this->processor->getRequestArguments()->getArgument($formName);
48
        $formArray = $this->getFormArray($formName);
49
50
        return $argument->setValue($formArray)->getValue();
51
    }
52
53
    /**
54
     * @param string $formName
55
     * @return array
56
     */
57
    protected function getFormArray($formName)
58
    {
59
        $formArray = $this->processor->getRequest()->getArgument($formName);
60
        $formArray = is_array($formArray)
61
            ? $formArray
62
            : [];
63
64
        $identifier = $this->getFormIdentifier();
65
66
        if ($identifier) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $identifier 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...
67
            /*
68
             * Forcing the identity of the object, that will be handled
69
             * internally by Extbase.
70
             */
71
            $formArray['__identity'] = $identifier;
72
            $propertyMappingConfiguration = $this->processor->getRequestArguments()->getArgument($formName)->getPropertyMappingConfiguration();
73
            $propertyMappingConfiguration->setTypeConverterOption(
74
                PersistentObjectConverter::class,
75
                PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED,
76
                true
77
            );
78
        } else {
79
            // Making sure no identity has been forced by the user.
80
            unset($formArray['__identity']);
81
        }
82
83
        return $formArray;
84
    }
85
86
    /**
87
     * Fetches the form identifier from the metadata, using the hash passed to
88
     * the request.
89
     *
90
     * @return string
91
     */
92
    protected function getFormIdentifier()
93
    {
94
        $request = $this->processor->getRequest();
95
96
        if ($request->hasArgument('fz-hash')) {
97
            $hash = $request->getArgument('fz-hash');
98
            $metaData = $this->formMetadataRepository->findOneByHash($hash);
99
100
            if ($metaData) {
101
                return $metaData->getIdentifier();
102
            }
103
        }
104
105
        return null;
106
    }
107
108
    /**
109
     * @param FormMetadataRepository $formMetadataRepository
110
     */
111
    public function injectFormMetadataRepository(FormMetadataRepository $formMetadataRepository)
112
    {
113
        $this->formMetadataRepository = $formMetadataRepository;
114
    }
115
}
116