Completed
Push — middleware-wip ( 5cfd03...f2f782 )
by Romain
05:53
created

AbstractFormDefinition::getDefinitionState()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
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
        attachParent as private attachParentInternal;
25
        attachParents as private attachParentsInternal;
26
    }
27
28
    /**
29
     * @var bool
30
     */
31
    private $parentsAttached = false;
32
33
    /**
34
     * @todo
35
     */
36
    protected function checkDefinitionFreezeState()
37
    {
38
        if ($this->isDefinitionFrozen()) {
39
            $this->throwFrozenStateException();
40
        }
41
    }
42
43
    /**
44
     * @return bool
45
     */
46
    protected function isDefinitionFrozen()
47
    {
48
        return $this->getDefinitionState()
49
            && $this->getDefinitionState()->isDefinitionFrozen();
50
    }
51
52
    /**
53
     * @return FormDefinitionState
54
     */
55
    protected function getDefinitionState()
56
    {
57
        if ($this instanceof FormDefinition) {
58
            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...
59
        } else {
60
            return $this->withFirstParent(FormDefinition::class, function (FormDefinition $formDefinition) {
61
                return $formDefinition->getState();
62
            });
63
        }
64
    }
65
66
    /**
67
     * @param object[] $parents
68
     */
69
    public function attachParents(array $parents)
70
    {
71
        if (false === $this->parentsAttached) {
72
            $this->attachParentsInternal($parents);
0 ignored issues
show
Documentation Bug introduced by
The method attachParentsInternal 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...
73
            $this->parentsAttached = true;
74
        }
75
    }
76
77
    /**
78
     * @param object $parent
79
     * @param bool   $direct
80
     */
81
    public function attachParent($parent, $direct = true)
82
    {
83
        if (false === $this->parentsAttached) {
84
            $this->attachParentInternal($parent, $direct);
0 ignored issues
show
Documentation Bug introduced by
The method attachParentInternal 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...
85
        }
86
    }
87
88
    /**
89
     * @todo
90
     *
91
     * @param mixed $name
92
     * @param array $arguments
93
     * @return mixed
94
     * @throws MethodNotFoundException
95
     */
96
    public function __call($name, $arguments)
97
    {
98
        if ($this->isDefinitionFrozen()) {
99
            if ('get' !== substr($name, 0, 3)) {
100
                $this->throwFrozenStateException();
101
            }
102
103
            try {
104
                $result = $this->handleMagicMethods($name, $arguments);
105
            } catch (MethodNotFoundException $exception) {
106
                throw $exception;
107
            }
108
        } else {
109
            $result = $this->handleMagicMethods($name, $arguments);
110
        }
111
112
        return $result;
113
    }
114
115
    /**
116
     * @todo
117
     */
118
    private function throwFrozenStateException()
119
    {
120
        throw new \Exception('FROZEN!'); // @todo
121
    }
122
}
123