Completed
Push — feature/middleware ( 308431...0268bb )
by Romain
02:25
created

FormObjectRequestData::throwFormDataException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\Service;
15
16
use Romm\Formz\Exceptions\EntryNotFoundException;
17
use Romm\Formz\Exceptions\InvalidArgumentValueException;
18
use TYPO3\CMS\Extbase\Security\Cryptography\HashService;
19
use TYPO3\CMS\Extbase\Security\Exception;
20
21
class FormObjectRequestData
22
{
23
    /**
24
     * @var HashService
25
     */
26
    protected $hashService;
27
28
    /**
29
     * @var array
30
     */
31
    protected $data = [];
32
33
    /**
34
     * @var string
35
     */
36
    protected $formHash;
37
38
    /**
39
     * @var string
40
     */
41
    protected $contentObjectTable;
42
43
    /**
44
     * @var int
45
     */
46
    protected $contentObjectUid;
47
48
    /**
49
     * The given argument must be a string, hashed with the hash service, and
50
     * must contain an instance of this class. The data will be fetched from the
51
     * instance, and injected in the actual class.
52
     *
53
     * @param string $hash
54
     */
55
    public function fillFromHash($hash)
56
    {
57
        try {
58
            /** @var FormObjectRequestData $requestData */
59
            $requestData = $this->hashService->validateAndStripHmac($hash);
60
        } catch (Exception $exception) {
61
            $this->throwFormDataException();
62
        }
63
64
        $requestData = base64_decode($requestData);
65
66
        if (false === $requestData) {
67
            $this->throwFormDataException();
68
        }
69
70
        $requestData = unserialize($requestData);
71
72
        if (false === $requestData) {
73
            $this->throwFormDataException();
74
        }
75
76
        foreach ($requestData as $key => $value) {
77
            $this->$key = $value;
78
        }
79
    }
80
81
    /**
82
     * @param string $key
83
     * @param mixed $value
84
     */
85
    public function addData($key, $value)
86
    {
87
        $this->data[$key] = $value;
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function getData()
94
    {
95
        return $this->data;
96
    }
97
98
    /**
99
     * @param string $key
100
     * @return bool
101
     */
102
    public function hasData($key)
103
    {
104
        return true === isset($this->data[$key]);
105
    }
106
107
    /**
108
     * @param string $key
109
     * @return mixed
110
     * @throws EntryNotFoundException
111
     */
112
    public function getDataValue($key)
113
    {
114
        if (false === $this->hasData($key)) {
115
            throw EntryNotFoundException::formRequestDataNotFound($key);
116
        }
117
118
        return $this->data[$key];
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getFormHash()
125
    {
126
        return $this->formHash;
127
    }
128
129
    /**
130
     * @param string $formHash
131
     */
132
    public function setFormHash($formHash)
133
    {
134
        $this->formHash = $formHash;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getContentObjectTable()
141
    {
142
        return $this->contentObjectTable;
143
    }
144
145
    /**
146
     * @param string $contentObjectTable
147
     */
148
    public function setContentObjectTable($contentObjectTable)
149
    {
150
        $this->contentObjectTable = $contentObjectTable;
151
    }
152
153
    /**
154
     * @return int
155
     */
156
    public function getContentObjectUid()
157
    {
158
        return (int)$this->contentObjectUid;
159
    }
160
161
    /**
162
     * @param int $contentObjectUid
163
     */
164
    public function setContentObjectUid($contentObjectUid)
165
    {
166
        $this->contentObjectUid = (int)$contentObjectUid;
167
    }
168
169
    /**
170
     * @param HashService $hashService
171
     */
172
    public function injectHashService(HashService $hashService)
173
    {
174
        $this->hashService = $hashService;
175
    }
176
177
    /**
178
     * @return array
179
     */
180
    public function toArray()
181
    {
182
        $result = [];
183
184
        $properties = get_object_vars($this);
185
        unset($properties['hashService']);
186
187
        foreach (array_keys($properties) as $propertyName) {
188
            $result[$propertyName] = $this->$propertyName;
189
        }
190
191
        return $result;
192
    }
193
194
    /**
195
     * @throws InvalidArgumentValueException
196
     */
197
    protected function throwFormDataException()
198
    {
199
        throw InvalidArgumentValueException::formDataFetchingError();
200
    }
201
}
202