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

FormObject   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 5
dl 0
loc 155
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A getClassName() 0 4 1
A getDefinition() 0 4 1
A getDefinitionValidationResult() 0 4 1
A getForm() 0 4 1
A hasForm() 0 4 1
A setForm() 0 8 2
A formWasSubmitted() 0 4 2
A formWasValidated() 0 4 2
A getFormResult() 0 4 1
A getFormHash() 0 4 1
A getObjectHash() 0 4 1
A getProxy() 0 8 2
A createProxy() 0 4 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\Error\FormResult;
17
use Romm\Formz\Exceptions\DuplicateEntryException;
18
use Romm\Formz\Exceptions\PropertyNotAccessibleException;
19
use Romm\Formz\Form\Definition\FormDefinition;
20
use Romm\Formz\Form\FormInterface;
21
use TYPO3\CMS\Extbase\Error\Result;
22
23
/**
24
 * This is the object representation of a form. In here we can manage which
25
 * properties the form does have, its configuration, and more.
26
 */
27
class FormObject
28
{
29
    /**
30
     * @var string
31
     */
32
    protected $name;
33
34
    /**
35
     * @var FormObjectStatic
36
     */
37
    protected $static;
38
39
    /**
40
     * @var FormObjectProxy
41
     */
42
    protected $proxy;
43
44
    /**
45
     * You should never create a new instance of this class directly, use the
46
     * `FormObjectFactory->getInstanceFromClassName()` function instead.
47
     *
48
     * @param string           $name
49
     * @param FormObjectStatic $static
50
     */
51
    public function __construct($name, FormObjectStatic $static)
52
    {
53
        $this->name = $name;
54
        $this->static = $static;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getName()
61
    {
62
        return $this->name;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getClassName()
69
    {
70
        return $this->static->getClassName();
71
    }
72
73
    /**
74
     * @return FormDefinition
75
     */
76
    public function getDefinition()
77
    {
78
        return $this->static->getDefinition();
79
    }
80
81
    /**
82
     * @return Result
83
     */
84
    public function getDefinitionValidationResult()
85
    {
86
        return $this->static->getDefinitionValidationResult();
87
    }
88
89
    /**
90
     * @return FormInterface
91
     */
92
    public function getForm()
93
    {
94
        return $this->getProxy()->getForm();
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function hasForm()
101
    {
102
        return $this->proxy !== null;
103
    }
104
105
    /**
106
     * @param FormInterface $form
107
     * @throws DuplicateEntryException
108
     */
109
    public function setForm(FormInterface $form)
110
    {
111
        if ($this->proxy) {
112
            throw DuplicateEntryException::formInstanceAlreadyAdded($this);
113
        }
114
115
        $this->proxy = $this->createProxy($form);
116
    }
117
118
    /**
119
     * @return bool
120
     */
121
    public function formWasSubmitted()
122
    {
123
        return $this->hasForm() && $this->getProxy()->formWasSubmitted();
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    public function formWasValidated()
130
    {
131
        return $this->hasForm() && $this->getProxy()->formWasValidated();
132
    }
133
134
    /**
135
     * @return FormResult
136
     */
137
    public function getFormResult()
138
    {
139
        return $this->getProxy()->getFormResult();
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getFormHash()
146
    {
147
        return $this->getProxy()->getFormHash();
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getObjectHash()
154
    {
155
        return $this->static->getObjectHash();
156
    }
157
158
    /**
159
     * @return FormObjectProxy
160
     * @throws PropertyNotAccessibleException
161
     */
162
    protected function getProxy()
163
    {
164
        if (null === $this->proxy) {
165
            throw PropertyNotAccessibleException::formInstanceNotSet();
166
        }
167
168
        return $this->proxy;
169
    }
170
171
    /**
172
     * Wrapper for unit tests.
173
     *
174
     * @param FormInterface $form
175
     * @return FormObjectProxy
176
     */
177
    protected function createProxy(FormInterface $form)
178
    {
179
        return FormObjectFactory::get()->getProxy($form);
180
    }
181
}
182