Completed
Push — middleware-wip ( 357c0d...3d734d )
by Romain
02:39
created

FormObjectRequestData   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 94
rs 10
c 0
b 0
f 0

7 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 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;
15
16
use TYPO3\CMS\Extbase\Security\Cryptography\HashService;
17
18
class FormObjectRequestData
19
{
20
    /**
21
     * @var HashService
22
     */
23
    protected $hashService;
24
25
    /**
26
     * @var array
27
     */
28
    protected $data = [];
29
30
    /**
31
     * The given argument must be a string, hashed with the hash service, and
32
     * must contain an instance of this class. The data will be fetched from the
33
     * instance, and injected in the actual class.
34
     *
35
     * @param string $hash
36
     */
37
    public function fillFromHash($hash)
38
    {
39
        /** @var FormObjectRequestData $requestData */
40
        $requestData = $this->hashService->validateAndStripHmac($hash);
41
        $requestData = unserialize(base64_decode($requestData));
42
43
        foreach ($requestData as $key => $value) {
44
            $this->$key = $value;
45
        }
46
    }
47
48
    /**
49
     * @param string $key
50
     * @param mixed $value
51
     */
52
    public function addData($key, $value)
53
    {
54
        $this->data[$key] = $value;
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function getData()
61
    {
62
        return $this->data;
63
    }
64
65
    /**
66
     * @param string $key
67
     * @return bool
68
     */
69
    public function hasData($key)
70
    {
71
        return true === isset($this->data[$key]);
72
    }
73
74
    /**
75
     * @param string $key
76
     * @return mixed
77
     */
78
    public function getDataValue($key)
79
    {
80
        if (false === $this->hasData($key)) {
81
            // @todo exception
82
        }
83
84
        return $this->data[$key];
85
    }
86
87
    /**
88
     * @param HashService $hashService
89
     */
90
    public function injectHashService(HashService $hashService)
91
    {
92
        $this->hashService = $hashService;
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function toArray()
99
    {
100
        $result = [];
101
102
        $properties = get_object_vars($this);
103
        unset($properties['hashService']);
104
105
        foreach (array_keys($properties) as $propertyName) {
106
            $result[$propertyName] = $this->$propertyName;
107
        }
108
109
        return $result;
110
    }
111
}
112