Completed
Push — feature/version-2 ( 4aa9b8...6dfc1b )
by Romain
02:16
created

FormObjectStatic   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 4
dl 0
loc 94
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassName() 0 4 1
A __construct() 0 6 1
A getObjectHash() 0 8 2
A getDefinition() 0 7 1
A getDefinitionValidationResult() 0 4 1
A calculateObjectHash() 0 10 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\FormObject;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Form\Definition\FormDefinition;
18
use Romm\Formz\Form\FormObject\Definition\FormDefinitionObject;
19
use Romm\Formz\Form\FormObject\Service\FormObjectConfiguration;
20
use Romm\Formz\Service\HashService;
21
use TYPO3\CMS\Extbase\Error\Result;
22
23
class FormObjectStatic
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $className;
29
30
    /**
31
     * @var FormDefinitionObject
32
     */
33
    protected $definition;
34
35
    /**
36
     * @var string
37
     */
38
    protected $objectHash;
39
40
    /**
41
     * @var FormObjectConfiguration
42
     */
43
    protected $configurationService;
44
45
    /**
46
     * @param string               $className
47
     * @param FormDefinitionObject $definition
48
     */
49
    public function __construct($className, FormDefinitionObject $definition)
50
    {
51
        $this->className = $className;
52
        $this->definition = $definition;
53
        $this->configurationService = Core::instantiate(FormObjectConfiguration::class, $this, $definition);
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getClassName()
60
    {
61
        return $this->className;
62
    }
63
64
    /**
65
     * @return FormDefinition
66
     */
67
    public function getDefinition()
68
    {
69
        /** @var FormDefinition $configuration */
70
        $configuration = $this->definition->getObject(true);
71
72
        return $configuration;
73
    }
74
75
    /**
76
     * This function will merge and return the validation results of both the
77
     * global FormZ configuration object, and this form configuration object.
78
     *
79
     * @return Result
80
     */
81
    public function getDefinitionValidationResult()
82
    {
83
        return $this->configurationService->getConfigurationValidationResult();
84
    }
85
86
    /**
87
     * Returns the hash of the form object, which should be calculated only once
88
     * for performance concerns.
89
     *
90
     * @return string
91
     */
92
    public function getObjectHash()
93
    {
94
        if (null === $this->objectHash) {
95
            $this->objectHash = $this->calculateObjectHash();
96
        }
97
98
        return $this->objectHash;
99
    }
100
101
    /**
102
     * Returns the calculated hash of the form object.
103
     *
104
     * @return string
105
     */
106
    protected function calculateObjectHash()
107
    {
108
        /*
109
         * Triggering the validation result calculation, to be sure the values
110
         * will be in the serialization string.
111
         */
112
        $this->getDefinitionValidationResult();
113
114
        return HashService::get()->getHash(serialize($this));
115
    }
116
}
117