Passed
Push — master ( e51dd6...1991ec )
by Ralf
12:21
created

DocumentForm::getCsrfToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace EWW\Dpf\Domain\Model;
4
5
use Exception;
6
use TypeError;
7
use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
8
9
/*
10
 * This file is part of the TYPO3 CMS project.
11
 *
12
 * It is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU General Public License, either version 2
14
 * of the License, or any later version.
15
 *
16
 * For the full copyright and license information, please read the
17
 * LICENSE.txt file that was distributed with this source code.
18
 *
19
 * The TYPO3 project - inspiring people to share!
20
 */
21
22
class DocumentForm extends AbstractFormElement
23
{
24
25
    /**
26
     * @var string CSRF token for this form
27
     */
28
    private $csrfToken;
29
30
    /**
31
     *
32
     * @var integer
33
     */
34
    protected $documentUid;
35
36
    /**
37
     *
38
     * @var boolean
39
     */
40
    protected $primaryFileMandatory;
41
42
    /**
43
     *
44
     * @var string
45
     */
46
    protected $fedoraPid;
47
48
    /**
49
     *
50
     * @var \EWW\Dpf\Domain\Model\File
51
     */
52
    protected $primaryFile;
53
54
    /**
55
     *
56
     * @var array
57
     */
58
    protected $secondaryFiles;
59
60
    /**
61
     *
62
     * @var array
63
     */
64
    protected $deletedFiles;
65
66
    /**
67
     *
68
     * @var array
69
     */
70
    protected $newFiles;
71
72
    /**
73
     *
74
     * @var string
75
     */
76
    protected $objectState;
77
78
    /**
79
     *
80
     * @var boolean
81
     */
82
    protected $valid = false;
83
84
    /**
85
     *
86
     * @var string
87
     */
88
    protected $processNumber;
89
90
    /**
91
     * @var bool
92
     */
93
    protected $temporary;
94
95
    /**
96
     * @var string
97
     */
98
    protected $comment = '';
99
100
    /**
101
     * Assign and persist CSRF token for later form validation.
102
     *
103
     * @param string $csrfToken
104
     */
105
    public function generateCsrfToken()
106
    {
107
        $formProtection = FormProtectionFactory::get();
108
        $this->csrfToken = $formProtection->generateToken('DocumentForm', 'construct', 'DocumentForm');
109
        $formProtection->persistSessionToken();
110
    }
111
112
    /**
113
     * Set the CSRF token for this form
114
     *
115
     * Used when creating a new instance from request form data.
116
     *
117
     * @param string $csrfToken CSRF token to set
118
     * @throws Exception if the given string is empty.
119
     * @throws TypeError if the given string is null
120
     */
121
    public function setCsrfToken(string $csrfToken)
122
    {
123
        if ($csrfToken === "")
124
        {
125
            throw new Exception("A forms CSRF token cannot be empty");
126
        }
127
        $this->csrfToken = $csrfToken;
128
    }
129
130
131
    /**
132
     * Returns the CSRF token of this form
133
     *
134
     * @return string CSRF token for this form
135
     */
136
    public function getCsrfToken()
137
    {
138
        return $this->csrfToken;
139
    }
140
141
142
    /**
143
     * Validates this forms assigned CSRF token with token stored in the TYPO3 session.
144
     *
145
     * @return bool True, is CSRF token is considered valid. False if the token is invalid or missing.
146
     */
147
    public function hasValidCsrfToken()
148
    {
149
        $formProtection = FormProtectionFactory::get();
150
        return $formProtection->validateToken($this->csrfToken, 'DocumentForm', 'construct', 'DocumentForm');
151
    }
152
153
    /**
154
     *
155
     * @return integer
156
     */
157
    public function getDocumentUid()
158
    {
159
        return $this->documentUid;
160
    }
161
162
    /**
163
     *
164
     * @param integer $documentUid
165
     */
166
    public function setDocumentUid($documentUid)
167
    {
168
        $this->documentUid = $documentUid;
169
    }
170
171
    /**
172
     *
173
     * @return boolean
174
     */
175
    public function getPrimaryFileMandatory()
176
    {
177
        return $this->primaryFileMandatory;
178
    }
179
180
    /**
181
     *
182
     * @param boolean $primaryFileMandatory
183
     */
184
    public function setPrimaryFileMandatory($primaryFileMandatory)
185
    {
186
        $this->primaryFileMandatory = boolval($primaryFileMandatory);
187
    }
188
189
    /**
190
     *
191
     * @return string
192
     */
193
    public function getFedoraPid()
194
    {
195
        return $this->fedoraPid;
196
    }
197
198
    /**
199
     *
200
     * @param string $fedoraPid
201
     */
202
    public function setFedoraPid($fedoraPid)
203
    {
204
        $this->fedoraPid = $fedoraPid;
205
    }
206
207
    /**
208
     *
209
     * @param type \EWW\Dpf\Domain\Model\File $primaryFile
210
     */
211
    public function setPrimaryFile($primaryFile)
212
    {
213
        $this->primaryFile = $primaryFile;
214
    }
215
216
    /**
217
     *
218
     * @return \EWW\Dpf\Domain\Model\File
219
     */
220
    public function getPrimaryFile()
221
    {
222
        return $this->primaryFile;
223
    }
224
225
    public function setSecondaryFiles($secondaryFiles)
226
    {
227
        $this->secondaryFiles = $secondaryFiles;
228
    }
229
230
    public function getSecondaryFiles()
231
    {
232
        return $this->secondaryFiles;
233
    }
234
235
    public function getDeletedFiles()
236
    {
237
        return $this->deletedFiles;
238
    }
239
240
    public function setDeletedFiles($deletedFiles)
241
    {
242
        $this->deletedFiles = $deletedFiles;
243
    }
244
245
    public function getNewFiles()
246
    {
247
        return $this->newFiles;
248
    }
249
250
    public function setNewFiles($newFiles)
251
    {
252
        $this->newFiles = $newFiles;
253
    }
254
255
    /**
256
     * @return bool
257
     */
258
    public function getValid()
259
    {
260
        return $this->valid;
261
    }
262
263
    /**
264
     * @param bool $valid
265
     */
266
    public function setValid($valid)
267
    {
268
        $this->valid = boolval($valid);
269
    }
270
271
    public function getNewFileNames()
272
    {
273
        $fileNames = array();
274
        foreach ($this->getNewFiles() as $file) {
275
            $fileNames[] = $file->getTitle();
276
        }
277
        return $fileNames;
278
    }
279
280
    /**
281
     * Sets the process number
282
     *
283
     * @return string
284
     */
285
    public function getProcessNumber()
286
    {
287
        return $this->processNumber;
288
    }
289
290
    /**
291
     * Gets the process number
292
     *
293
     * @param string $processNumber
294
     */
295
    public function setProcessNumber($processNumber)
296
    {
297
        $this->processNumber = $processNumber;
298
    }
299
300
    /**
301
     * Returns if a document is a temporary document.
302
     *
303
     * @return bool
304
     */
305
    public function isTemporary()
306
    {
307
        return $this->temporary;
308
    }
309
310
    /**
311
     * Sets if a document is a temporary document or not.
312
     * @param bool $temporary
313
     */
314
    public function setTemporary($temporary)
315
    {
316
        $this->temporary = boolval($temporary);
317
    }
318
319
    /**
320
     * @return string
321
     */
322
    public function getComment()
323
    {
324
        return $this->comment;
325
    }
326
327
    /**
328
     * @param string $comment
329
     */
330
    public function setComment($comment)
331
    {
332
        $this->comment = $comment;
333
    }
334
}
335