Completed
Push — development ( f488fc...915d6a )
by Romain
04:48 queued 02:48
created

FormViewHelperService::activateFormContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
/*
3
 * 2016 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\Service\ViewHelper;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Error\FormResult;
18
use Romm\Formz\Exceptions\DuplicateEntryException;
19
use Romm\Formz\Form\FormInterface;
20
use Romm\Formz\Form\FormObject;
21
use Romm\Formz\Validation\Validator\Form\DefaultFormValidator;
22
use TYPO3\CMS\Core\SingletonInterface;
23
use TYPO3\CMS\Extbase\Mvc\Request;
24
25
/**
26
 * This class contains methods that help view helpers to manipulate data and
27
 * know more things concerning the current form state.
28
 *
29
 * It is mainly configured inside the `FormViewHelper`, and used in other
30
 * view helpers.
31
 */
32
class FormViewHelperService implements SingletonInterface
33
{
34
    /**
35
     * @var bool
36
     */
37
    protected $formContext = false;
38
39
    /**
40
     * @var array|FormInterface
41
     */
42
    protected $formInstance;
43
44
    /**
45
     * @var bool
46
     */
47
    protected $formWasSubmitted = false;
48
49
    /**
50
     * @var FormResult
51
     */
52
    protected $formResult;
53
54
    /**
55
     * @var FormObject
56
     */
57
    protected $formObject;
58
59
    /**
60
     * Reset every state that can be used by this service.
61
     */
62
    public function resetState()
63
    {
64
        $this->formContext = false;
65
        $this->formWasSubmitted = false;
66
        $this->formObject = null;
67
        $this->formInstance = null;
68
        $this->formResult = null;
69
    }
70
71
    /**
72
     * Will activate the form context, changing the result returned by the
73
     * function `formContextExists()`.
74
     *
75
     * @return FormViewHelperService
76
     * @throws DuplicateEntryException
77
     */
78
    public function activateFormContext()
79
    {
80
        if (true === $this->formContext) {
81
            throw DuplicateEntryException::duplicatedFormContext();
82
        }
83
84
        $this->formContext = true;
85
86
        return $this;
87
    }
88
89
    /**
90
     * Returns `true` if the `FormViewHelper` context exists.
91
     *
92
     * @return bool
93
     */
94
    public function formContextExists()
95
    {
96
        return $this->formContext;
97
    }
98
99
    /**
100
     * This function will check and inject the form instance and its submission
101
     * result.
102
     *
103
     * @param string              $formName
104
     * @param Request             $originalRequest
105
     * @param array|FormInterface $formInstance
106
     */
107
    public function setUpData($formName, $originalRequest, $formInstance)
108
    {
109
        if (null !== $originalRequest
110
            && $originalRequest->hasArgument($formName)
111
        ) {
112
            /** @var array $formInstance */
113
            $formInstance = $originalRequest->getArgument($formName);
114
115
            $this->setFormInstance($formInstance);
116
            $this->setFormResult($this->formObject->getLastValidationResult());
117
            $this->markFormAsSubmitted();
118
        } elseif (null !== $formInstance) {
119
            $formValidator = $this->getFormValidator($formName);
120
            $formRequestResult = $formValidator->validateWithoutSavingResults($formInstance);
0 ignored issues
show
Bug introduced by
It seems like $formInstance defined by parameter $formInstance on line 107 can also be of type array; however, Romm\Formz\Validation\Va...eWithoutSavingResults() does only seem to accept object<Romm\Formz\Form\FormInterface>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
121
122
            $this->setFormInstance($formInstance);
123
            $this->setFormResult($formRequestResult);
124
        }
125
    }
126
127
    /**
128
     * Will mark the form as submitted (change the result returned by the
129
     * function `formWasSubmitted()`).
130
     */
131
    public function markFormAsSubmitted()
132
    {
133
        $this->formWasSubmitted = true;
134
    }
135
136
    /**
137
     * Returns `true` if the form was submitted by the user.
138
     *
139
     * @return bool
140
     */
141
    public function formWasSubmitted()
142
    {
143
        return $this->formWasSubmitted;
144
    }
145
146
    /**
147
     * @return array|FormInterface
148
     */
149
    public function getFormInstance()
150
    {
151
        return $this->formInstance;
152
    }
153
154
    /**
155
     * If the form was submitted by the user, contains the array containing the
156
     * submitted values.
157
     *
158
     * @param array|FormInterface $formInstance
159
     */
160
    public function setFormInstance($formInstance)
161
    {
162
        $this->formInstance = $formInstance;
163
    }
164
165
    /**
166
     * @return FormResult
167
     */
168
    public function getFormResult()
169
    {
170
        return $this->formResult;
171
    }
172
173
    /**
174
     * @param FormResult $formResult
175
     */
176
    public function setFormResult(FormResult $formResult)
177
    {
178
        $this->formResult = $formResult;
179
    }
180
181
    /**
182
     * @return FormObject
183
     */
184
    public function getFormObject()
185
    {
186
        return $this->formObject;
187
    }
188
189
    /**
190
     * @param FormObject $formObject
191
     */
192
    public function setFormObject(FormObject $formObject)
193
    {
194
        $this->formObject = $formObject;
195
    }
196
197
    /**
198
     * @param string $formName
199
     * @return DefaultFormValidator
200
     */
201
    protected function getFormValidator($formName)
202
    {
203
        return Core::instantiate(DefaultFormValidator::class, ['name' => $formName]);
204
    }
205
}
206