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

FormObjectProxy::getFormHash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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\Form\FormObject;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Domain\Model\FormMetadata;
18
use Romm\Formz\Error\FormResult;
19
use Romm\Formz\Form\FormInterface;
20
use Romm\Formz\Form\Service\FormObjectMetadata;
21
use Romm\Formz\Form\Service\FormObjectRequestData;
22
use Romm\Formz\Service\HashService;
23
24
class FormObjectProxy
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $name;
30
31
    /**
32
     * @var FormObject
33
     */
34
    protected $formObject;
35
36
    /**
37
     * @var FormInterface
38
     */
39
    protected $form;
40
41
    /**
42
     * @var FormObjectRequestData
43
     */
44
    protected $requestData;
45
46
    /**
47
     * @var string
48
     */
49
    protected $formHash;
50
51
    /**
52
     * @var bool
53
     */
54
    protected $formWasSubmitted = false;
55
56
    /**
57
     * @var FormResult
58
     */
59
    protected $formResult;
60
61
    /**
62
     * @var FormObjectMetadata
63
     */
64
    protected $metadata;
65
66
    /**
67
     * @param FormObject    $formObject
68
     * @param FormInterface $form
69
     */
70
    public function __construct(FormObject $formObject, FormInterface $form)
71
    {
72
        $this->formObject = $formObject;
73
        $this->form = $form;
74
75
        $this->metadata = Core::instantiate(FormObjectMetadata::class, $formObject);
76
    }
77
78
    /**
79
     * @return FormObjectRequestData
80
     */
81
    public function getRequestData()
82
    {
83
        return $this->requestData;
84
    }
85
86
    /**
87
     * @return FormInterface
88
     */
89
    public function getForm()
90
    {
91
        return $this->form;
92
    }
93
94
    /**
95
     * Will mark the form as submitted (change the result returned by the
96
     * function `formWasSubmitted()`).
97
     */
98
    public function markFormAsSubmitted()
99
    {
100
        $this->formWasSubmitted = true;
101
    }
102
103
    /**
104
     * Returns `true` if the form was submitted by the user.
105
     *
106
     * @return bool
107
     */
108
    public function formWasSubmitted()
109
    {
110
        return $this->formWasSubmitted;
111
    }
112
113
    /**
114
     * @return FormResult
115
     */
116
    public function getFormResult()
117
    {
118
        return $this->formResult;
119
    }
120
121
    /**
122
     * @return bool
123
     */
124
    public function hasFormResult()
125
    {
126
        return null !== $this->formResult;
127
    }
128
129
    /**
130
     * @param FormResult $formResult
131
     */
132
    public function setFormResult(FormResult $formResult)
133
    {
134
        $this->formResult = $formResult;
135
    }
136
137
    /**
138
     * @return FormMetadata
139
     */
140
    public function getMetadata()
141
    {
142
        return $this->metadata->getMetadata();
143
    }
144
145
    /**
146
     * @param FormMetadata $metadata
147
     */
148
    public function setMetadata(FormMetadata $metadata)
149
    {
150
        $this->metadata->setMetadata($metadata);
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function getFormHash()
157
    {
158
        if (null === $this->formHash) {
159
            $this->formHash = HashService::get()->getHash(uniqid(get_class($this->form)));
160
        }
161
162
        return $this->formHash;
163
    }
164
165
    /**
166
     * @param FormObjectRequestData $requestData
167
     */
168
    public function injectRequestData(FormObjectRequestData $requestData)
169
    {
170
        $this->requestData = $requestData;
171
    }
172
}
173