Configuration::calculateHash()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
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\Form\Form;
23
use Romm\Formz\Configuration\Settings\Settings;
24
use Romm\Formz\Configuration\View\View;
25
use Romm\Formz\Exceptions\DuplicateEntryException;
26
use Romm\Formz\Form\FormObject;
27
use Romm\Formz\Service\CacheService as InternalCacheService;
28
use Romm\Formz\Service\HashService;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
class Configuration extends AbstractFormzConfiguration implements ConfigurationObjectInterface
32
{
33
    use DefaultConfigurationObjectTrait;
34
    use ArrayConversionTrait;
35
36
    /**
37
     * @var \Romm\Formz\Configuration\Settings\Settings
38
     */
39
    protected $settings;
40
41
    /**
42
     * @var FormObject[]
43
     */
44
    protected $forms = [];
45
46
    /**
47
     * @var \Romm\Formz\Configuration\View\View
48
     */
49
    protected $view;
50
51
    /**
52
     * @var string
53
     */
54
    protected $hash;
55
56
    /**
57
     * Constructor.
58
     */
59
    public function __construct()
60
    {
61
        $this->settings = GeneralUtility::makeInstance(Settings::class);
62
        $this->view = GeneralUtility::makeInstance(View::class);
63
    }
64
65
    /**
66
     * Will initialize correctly the configuration object settings.
67
     *
68
     * @return ServiceFactory
69
     */
70
    public static function getConfigurationObjectServices()
71
    {
72
        return ServiceFactory::getInstance()
73
            ->attach(ServiceInterface::SERVICE_CACHE)
74
            ->with(ServiceInterface::SERVICE_CACHE)
75
            ->setOption(CacheService::OPTION_CACHE_NAME, InternalCacheService::CONFIGURATION_OBJECT_CACHE_IDENTIFIER)
76
            ->setOption(CacheService::OPTION_CACHE_BACKEND, InternalCacheService::get()->getBackendCache())
77
            ->attach(ServiceInterface::SERVICE_PARENTS)
78
            ->attach(ServiceInterface::SERVICE_DATA_PRE_PROCESSOR)
79
            ->attach(ServiceInterface::SERVICE_MIXED_TYPES);
80
    }
81
82
    /**
83
     * @return Settings
84
     */
85
    public function getSettings()
86
    {
87
        return $this->settings;
88
    }
89
90
    /**
91
     * Adds a form to the forms list of this FormZ configuration. Note that this
92
     * function will also handle the parent service from the
93
     * `configuration_object` extension.
94
     *
95
     * @param FormObject $form
96
     * @throws DuplicateEntryException
97
     */
98
    public function addForm(FormObject $form)
99
    {
100
        if (true === $this->hasForm($form->getClassName(), $form->getName())) {
101
            throw DuplicateEntryException::formWasAlreadyRegistered($form);
102
        }
103
104
        $form->getConfiguration()->setParents([$this]);
0 ignored issues
show
Deprecated Code introduced by
The method Romm\ConfigurationObject...entsTrait::setParents() has been deprecated with message: This function is deprecated and will be removed in v2! Use function `addParents()` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
105
106
        $this->forms[$form->getClassName()][$form->getName()] = $form;
107
    }
108
109
    /**
110
     * @param string $className
111
     * @param string $name
112
     * @return bool
113
     */
114
    public function hasForm($className, $name)
115
    {
116
        return true === isset($this->forms[$className][$name]);
117
    }
118
119
    /**
120
     * @param string $className
121
     * @param string $name
122
     * @return null|Form
123
     */
124
    public function getForm($className, $name)
125
    {
126
        return ($this->hasForm($className, $name))
127
            ? $this->forms[$className][$name]
128
            : null;
129
    }
130
131
    /**
132
     * @return View
133
     */
134
    public function getView()
135
    {
136
        return $this->view;
137
    }
138
139
    /**
140
     * Calculates a hash for this configuration, which can be used as a unique
141
     * identifier. It should be called once, before the configuration is put in
142
     * cache, so it is not needed to call it again after being fetched from
143
     * cache.
144
     */
145
    public function calculateHash()
146
    {
147
        $fullArray = $this->toArray();
148
        $configurationArray = [
149
            'view'     => $fullArray['view'],
150
            'settings' => $fullArray['settings']
151
        ];
152
153
        $this->hash = HashService::get()->getHash(serialize($configurationArray));
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    public function getHash()
160
    {
161
        return $this->hash;
162
    }
163
}
164