Completed
Push — feature/improve-form-definitio... ( 49be60...b9e9c1 )
by Romain
15:06
created

checkDefinitionFreezeState()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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\Definition;
15
16
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
17
use Romm\ConfigurationObject\Traits\ConfigurationObject\MagicMethodsTrait;
18
use Romm\Formz\Exceptions\PropertyNotAccessibleException;
19
20
class AbstractFormDefinitionComponent
21
{
22
    use MagicMethodsTrait {
23
        handlePropertyMagicMethod as handlePropertyMagicMethodInternal;
24
    }
25
    use ParentsTrait {
26
        attachParent as private attachParentInternal;
27
        attachParents as private attachParentsInternal;
28
    }
29
30
    /**
31
     * @var bool
32
     */
33
    private $parentsAttached = false;
34
35
    /**
36
     * This method is used by setter methods, and other methods which goal is to
37
     * modify a property value.
38
     *
39
     * It checks that the definition is not frozen, and if it is actually frozen
40
     * an exception is thrown.
41
     *
42
     * @throws PropertyNotAccessibleException
43
     */
44
    protected function checkDefinitionFreezeState()
45
    {
46
        if ($this->isDefinitionFrozen()) {
47
            $methodName = debug_backtrace()[1]['function'];
48
49
            throw PropertyNotAccessibleException::formDefinitionFrozenMethod(get_class($this), $methodName);
50
        }
51
    }
52
53
    /**
54
     * @return bool
55
     */
56
    protected function isDefinitionFrozen()
57
    {
58
        return $this->getDefinitionState()
59
            && $this->getDefinitionState()->isDefinitionFrozen();
60
    }
61
62
    /**
63
     * @return FormDefinitionState
64
     */
65
    protected function getDefinitionState()
66
    {
67
        if ($this instanceof FormDefinition) {
68
            return $this->getState();
0 ignored issues
show
Documentation Bug introduced by
The method getState does not exist on object<Romm\Formz\Form\D...ormDefinitionComponent>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
69
        } else {
70
            return $this->withFirstParent(
71
                FormDefinition::class,
72
                function (FormDefinition $formDefinition) {
73
                    return $formDefinition->getState();
74
                }
75
            );
76
        }
77
    }
78
79
    /**
80
     * Overrides the magic methods handling from the Configuration Object API.
81
     *
82
     * Blocks the parents feature once it has been used.
83
     *
84
     * @param object[] $parents
85
     */
86
    public function attachParents(array $parents)
87
    {
88
        if (false === $this->parentsAttached) {
89
            $this->attachParentsInternal($parents);
90
            $this->parentsAttached = true;
91
        }
92
    }
93
94
    /**
95
     * @see attachParents()
96
     *
97
     * @param object $parent
98
     * @param bool   $direct
99
     */
100
    public function attachParent($parent, $direct = true)
101
    {
102
        if (false === $this->parentsAttached) {
103
            $this->attachParentInternal($parent, $direct);
104
        }
105
    }
106
107
    /**
108
     * Overrides the magic methods handling from the Configuration Object API.
109
     *
110
     * A new check is added: if the definition is frozen, and a setter method is
111
     * called, an exception must be thrown.
112
     *
113
     * @param string $property
114
     * @param string $type
115
     * @param array  $arguments
116
     * @return mixed
117
     * @throws PropertyNotAccessibleException
118
     */
119
    protected function handlePropertyMagicMethod($property, $type, array $arguments)
120
    {
121
        if ($this->isDefinitionFrozen()
122
            && $type === 'set'
123
            && $this->isPropertyAccessible($property)
124
        ) {
125
            throw PropertyNotAccessibleException::formDefinitionFrozenProperty(get_class($this), $property);
126
        }
127
128
        return $this->handlePropertyMagicMethodInternal($property, $type, $arguments);
129
    }
130
}
131