Completed
Pull Request — master (#15)
by
unknown
02:10
created

Configuration::calculateHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/*
3
 * 2016 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\Core\Core;
26
use Romm\Formz\Exceptions\DuplicateEntryException;
27
use Romm\Formz\Form\FormObject;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
class Configuration extends AbstractFormzConfiguration implements ConfigurationObjectInterface
31
{
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 array
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
     * @throws \Exception
70
     */
71
    public static function getConfigurationObjectServices()
72
    {
73
        return ServiceFactory::getInstance()
74
            ->attach(ServiceInterface::SERVICE_CACHE)
75
            ->with(ServiceInterface::SERVICE_CACHE)
76
            ->setOption(CacheService::OPTION_CACHE_BACKEND, Core::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 new DuplicateEntryException(
102
                'The form "' . $form->getName() . '" of class "' . $form->getClassName() . '" was already registered. You can only register a form once. Check the function `hasForm()`.',
103
                1477255145
104
            );
105
        }
106
107
        /** @var Form $configuration */
108
        $configuration = $form->getConfigurationObject()->getObject(true);
109
110
        $configuration->setParents([$this]);
111
112
        $this->forms[$form->getClassName()][$form->getName()] = $form;
113
    }
114
115
    /**
116
     * @param string $className
117
     * @param string $name
118
     * @return bool
119
     */
120
    public function hasForm($className, $name)
121
    {
122
        return (true === isset($this->forms[$className][$name]));
123
    }
124
125
    /**
126
     * @param string $className
127
     * @param string $name
128
     * @return null|Form
129
     */
130
    public function getForm($className, $name)
131
    {
132
        return ($this->hasForm($className, $name))
133
            ? $this->forms[$className][$name]
134
            : null;
135
    }
136
137
    /**
138
     * @return View
139
     */
140
    public function getView()
141
    {
142
        return $this->view;
143
    }
144
145
    /**
146
     * Calculates a hash for this configuration, which can be used as a unique
147
     * identifier. It should be called once, before the configuration is put in
148
     * cache, so it is not needed to call it again after being fetched from
149
     * cache.
150
     */
151
    public function calculateHash()
152
    {
153
        $fullArray = $this->toArray();
154
        $configurationArray = [
155
            'view'     => $fullArray['view'],
156
            'settings' => $fullArray['settings']
157
        ];
158
159
        $this->hash = sha1(serialize($configurationArray));
0 ignored issues
show
Documentation Bug introduced by
It seems like sha1(serialize($configurationArray)) of type string is incompatible with the declared type array of property $hash.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    public function getHash()
166
    {
167
        return $this->hash;
168
    }
169
}
170