Completed
Push — middleware-wip ( 8fd059...e9d8ee )
by Romain
03:01
created

FormObjectHash   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 2
cbo 3
dl 0
loc 101
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getHash() 0 8 2
A resetHash() 0 4 1
A getFormIdentifierHash() 0 4 1
A getFormIdentifierObject() 0 6 1
A calculateHash() 0 4 1
A __wakeup() 0 4 1
A __sleep() 0 7 1
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\Service;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Form\FormObject;
18
use Romm\Formz\Form\Service\DataObject\FormIdentifierObject;
19
use Romm\Formz\Service\HashService;
20
21
class FormObjectHash
22
{
23
    /**
24
     * @var FormObject
25
     */
26
    protected $formObject;
27
28
    /**
29
     * @var string
30
     */
31
    protected $hash;
32
33
    /**
34
     * @var FormIdentifierObject
35
     */
36
    protected $identifierObject;
37
38
    /**
39
     * @param FormObject $formObject
40
     */
41
    public function __construct(FormObject $formObject)
42
    {
43
        $this->formObject = $formObject;
44
        $this->identifierObject = Core::instantiate(FormIdentifierObject::class, $formObject);
45
    }
46
47
    /**
48
     * Returns the hash of the form object, which should be calculated only once
49
     * for performance concerns.
50
     *
51
     * @return string
52
     */
53
    public function getHash()
54
    {
55
        if (null === $this->hash) {
56
            $this->hash = $this->calculateHash();
57
        }
58
59
        return $this->hash;
60
    }
61
62
    /**
63
     * Resets the hash, which will be calculated on next access.
64
     */
65
    public function resetHash()
66
    {
67
        $this->hash = null;
68
    }
69
70
    /**
71
     * @see \Romm\Formz\Form\FormObject::getFormIdentifierHash()
72
     *
73
     * @return string
74
     */
75
    public function getFormIdentifierHash()
76
    {
77
       return $this->identifierObject->getIdentifierHash();
78
    }
79
80
    /**
81
     * @see \Romm\Formz\Form\FormObject::getFormIdentifierObject()
82
     *
83
     * @param string $identifier
84
     * @return FormIdentifierObject
85
     */
86
    public function getFormIdentifierObject($identifier)
87
    {
88
        $this->identifierObject->analyzeIdentifierHash($identifier);
89
90
        return $this->identifierObject;
91
    }
92
93
    /**
94
     * Returns the calculated hash of the form object.
95
     *
96
     * @return string
97
     */
98
    protected function calculateHash()
99
    {
100
        return HashService::get()->getHash(serialize($this->formObject));
101
    }
102
103
    /**
104
     * Injects external dependencies.
105
     */
106
    public function __wakeup()
107
    {
108
        $this->identifierObject = Core::instantiate(FormIdentifierObject::class, $this->formObject);
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function __sleep()
115
    {
116
        $properties = get_object_vars($this);
117
        unset($properties['identifierObject']);
118
119
        return array_keys($properties);
120
    }
121
}
122