Completed
Push — feature/improve-form-definitio... ( 2a8834...8225fe )
by Romain
02:19
created

Configuration::markConfigurationAsFrozen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\ConfigurationObjectInterface;
17
use Romm\ConfigurationObject\Service\Items\Cache\CacheService;
18
use Romm\ConfigurationObject\Service\ServiceFactory;
19
use Romm\ConfigurationObject\Service\ServiceInterface;
20
use Romm\ConfigurationObject\Traits\ConfigurationObject\ArrayConversionTrait;
21
use Romm\ConfigurationObject\Traits\ConfigurationObject\DefaultConfigurationObjectTrait;
22
use Romm\Formz\Configuration\Settings\Settings;
23
use Romm\Formz\Configuration\View\View;
24
use Romm\Formz\Service\CacheService as InternalCacheService;
25
use Romm\Formz\Service\HashService;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28
class Configuration extends AbstractConfiguration implements ConfigurationObjectInterface
29
{
30
    use DefaultConfigurationObjectTrait;
31
    use ArrayConversionTrait;
32
33
    /**
34
     * @var \Romm\Formz\Configuration\Settings\Settings
35
     */
36
    protected $settings;
37
38
    /**
39
     * @var \Romm\Formz\Configuration\View\View
40
     */
41
    protected $view;
42
43
    /**
44
     * @var string
45
     */
46
    protected $hash;
47
48
    /**
49
     * @var bool
50
     */
51
    private $configurationIsFrozen = false;
52
53
    /**
54
     * Constructor.
55
     */
56
    public function __construct()
57
    {
58
        $this->settings = GeneralUtility::makeInstance(Settings::class);
59
        $this->settings->attachParent($this);
60
61
        $this->view = GeneralUtility::makeInstance(View::class);
62
        $this->view->attachParent($this);
63
    }
64
65
    /**
66
     * @return Settings
67
     */
68
    public function getSettings()
69
    {
70
        return $this->settings;
71
    }
72
73
    /**
74
     * @return View
75
     */
76
    public function getView()
77
    {
78
        return $this->view;
79
    }
80
81
    /**
82
     * Calculates a hash for this configuration, which can be used as a unique
83
     * identifier. It should be called once, before the configuration is put in
84
     * cache, so it is not needed to call it again after being fetched from
85
     * cache.
86
     */
87
    public function calculateHash()
88
    {
89
        return $this->hash = HashService::get()->getHash(serialize($this));
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getHash()
96
    {
97
        if (null === $this->hash) {
98
            $this->hash = $this->calculateHash();
99
        }
100
101
        return $this->hash;
102
    }
103
104
    /**
105
     * Marks the configuration as frozen: its properties can not be modified
106
     * after that.
107
     */
108
    public function markConfigurationAsFrozen()
109
    {
110
        $this->configurationIsFrozen = true;
111
    }
112
113
    /**
114
     * @return bool
115
     */
116
    public function isConfigurationFrozenRoot()
117
    {
118
        return $this->configurationIsFrozen;
119
    }
120
121
    /**
122
     * Will initialize correctly the configuration object settings.
123
     *
124
     * @return ServiceFactory
125
     */
126
    public static function getConfigurationObjectServices()
127
    {
128
        return ServiceFactory::getInstance()
129
            ->attach(ServiceInterface::SERVICE_CACHE)
130
            ->with(ServiceInterface::SERVICE_CACHE)
131
            ->setOption(CacheService::OPTION_CACHE_NAME, InternalCacheService::CONFIGURATION_OBJECT_CACHE_IDENTIFIER)
132
            ->setOption(CacheService::OPTION_CACHE_BACKEND, InternalCacheService::get()->getBackendCache())
133
            ->attach(ServiceInterface::SERVICE_PARENTS)
134
            ->attach(ServiceInterface::SERVICE_DATA_PRE_PROCESSOR)
135
            ->attach(ServiceInterface::SERVICE_MIXED_TYPES);
136
    }
137
}
138