Completed
Push — tmp-wip ( 51e3a7 )
by Romain
02:45
created

checkDefinitionFreezeState()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
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\Exceptions\MethodNotFoundException;
17
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
18
use Romm\ConfigurationObject\Traits\ConfigurationObject\MagicMethodsTrait;
19
20
class AbstractFormDefinition
21
{
22
    use MagicMethodsTrait;
23
    use ParentsTrait {
24
        setParents as private setParentsInternal;
25
    }
26
27
    /**
28
     * @var bool
29
     */
30
    private $parentsAttached = false;
31
32
    /**
33
     * @todo
34
     */
35
    protected function checkDefinitionFreezeState()
36
    {
37
        if ($this->isDefinitionFrozen()) {
38
            $this->throwFrozenStateException();
39
        }
40
    }
41
42
    /**
43
     * @return bool
44
     */
45
    protected function isDefinitionFrozen()
46
    {
47
        return $this->getDefinitionState()
48
            && $this->getDefinitionState()->isDefinitionFrozen();
49
    }
50
51
    /**
52
     * @return FormDefinitionState
53
     */
54
    protected function getDefinitionState()
55
    {
56
        if ($this instanceof FormDefinition) {
57
            return $this->getState();
0 ignored issues
show
Documentation Bug introduced by
The method getState does not exist on object<Romm\Formz\Form\D...AbstractFormDefinition>? 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...
58
        } else {
59
            return $this->withFirstParent(FormDefinition::class, function (FormDefinition $formDefinition) {
60
                return $formDefinition->getState();
61
            });
62
        }
63
    }
64
65
    /**
66
     * @param array $parents
67
     */
68
    public function setParents(array $parents)
69
    {
70
        if (false === $this->parentsAttached) {
71
            $this->parentsAttached = true;
72
            $this->setParentsInternal($parents);
73
        }
74
    }
75
76
    /**
77
     * @todo
78
     *
79
     * @param mixed $name
80
     * @param array $arguments
81
     * @return mixed
82
     * @throws MethodNotFoundException
83
     */
84
    public function __call($name, $arguments)
85
    {
86
        if ($this->isDefinitionFrozen()) {
87
            try {
88
                $result = $this->handleMagicMethods($name, $arguments);
89
            } catch (MethodNotFoundException $exception) {
90
                throw $exception;
91
            }
92
93
            if ('set' === substr($name, 0, 3)) {
94
                $this->throwFrozenStateException();
95
            }
96
        } else {
97
            $result = $this->handleMagicMethods($name, $arguments);
98
        }
99
100
        return $result;
101
    }
102
103
    /**
104
     * @todo
105
     */
106
    private function throwFrozenStateException()
107
    {
108
        throw new \Exception('FROZEN!'); // @todo
109
    }
110
}
111