Completed
Push — middleware-wip ( ffd957...48fd00 )
by Romain
02:53
created

FormObjectRequestData   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 158
rs 10

13 Methods

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