Completed
Push — middleware-wip ( f499dc...68caca )
by Romain
03:25
created

checkConfigurationFreezeState()   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\Configuration;
15
16
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
17
use Romm\ConfigurationObject\Traits\ConfigurationObject\MagicMethodsTrait;
18
use Romm\ConfigurationObject\ConfigurationObjectFactory;
19
use Romm\Formz\Exceptions\PropertyNotAccessibleException;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
abstract class AbstractConfiguration
23
{
24
    use MagicMethodsTrait {
25
        handlePropertyMagicMethod as handlePropertyMagicMethodInternal;
26
    }
27
    use ParentsTrait {
28
        attachParent as private attachParentInternal;
29
    }
30
31
    /**
32
     * @param string $path
33
     * @return string
34
     */
35
    protected function getAbsolutePath($path)
36
    {
37
        return GeneralUtility::getFileAbsFileName($path);
38
    }
39
40
    /**
41
     * This method is used by setter methods, and other methods which goal is to
42
     * modify a property value.
43
     *
44
     * It checks that the definition is not frozen, and if it is actually frozen
45
     * an exception is thrown.
46
     *
47
     * @throws PropertyNotAccessibleException
48
     */
49
    protected function checkConfigurationFreezeState()
50
    {
51
        if ($this->isConfigurationFrozen()) {
52
            $methodName = debug_backtrace()[1]['function'];
53
54
            throw PropertyNotAccessibleException::rootConfigurationFrozenMethod(get_class($this), $methodName);
55
        }
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    protected function isConfigurationFrozen()
62
    {
63
        return $this->getState()
64
            && $this->getState()->isFrozen();
65
    }
66
67
    /**
68
     * @return ConfigurationState
69
     */
70
    protected function getState()
71
    {
72
        if ($this->hasParent(Configuration::class)) {
73
            return $this->getFirstParent(Configuration::class)->getState();
74
        }
75
76
        return null;
77
    }
78
79
    /**
80
     * @param object $parent
81
     * @param bool   $direct
82
     */
83
    public function attachParent($parent, $direct = true)
84
    {
85
        $this->checkConfigurationFreezeState();
86
        $this->attachParentInternal($parent, $direct);
87
    }
88
89
    /**
90
     * Overrides the magic methods handling from the Configuration Object API: a
91
     * magic setter method must be accessible only for this API, otherwise an
92
     * exception must be thrown.
93
     *
94
     * @param string $property
95
     * @param string $type
96
     * @param array  $arguments
97
     * @return mixed
98
     * @throws PropertyNotAccessibleException
99
     */
100
    protected function handlePropertyMagicMethod($property, $type, array $arguments)
101
    {
102
        if ($type === 'set'
103
            && $this->isPropertyAccessible($property)
104
            && false === ConfigurationObjectFactory::getInstance()->isRunning()
105
        ) {
106
            throw PropertyNotAccessibleException::rootConfigurationFrozenProperty(get_class($this), $property);
107
        }
108
109
        return $this->handlePropertyMagicMethodInternal($property, $type, $arguments);
110
    }
111
}
112