Passed
Pull Request — master (#47)
by Romain
03:39
created

ChannelDefinition::fetchSettingsClassName()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 8
nop 1
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Core\Definition\Tree\Notification\Channel;
18
19
use CuyZ\Notiz\Core\Channel\Channel;
20
use CuyZ\Notiz\Core\Channel\Settings\ChannelSettings;
21
use CuyZ\Notiz\Core\Definition\Tree\AbstractDefinitionComponent;
22
use CuyZ\Notiz\Core\Exception\ClassNotFoundException;
23
use CuyZ\Notiz\Core\Exception\InvalidClassException;
24
use CuyZ\Notiz\Core\Exception\NotizException;
25
use CuyZ\Notiz\Service\LocalizationService;
26
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
27
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
28
use TYPO3\CMS\Extbase\Error\Error;
29
30
class ChannelDefinition extends AbstractDefinitionComponent implements DataPreProcessorInterface
31
{
32
    /**
33
     * @var string
34
     *
35
     * @validate NotEmpty
36
     */
37
    protected $identifier;
38
39
    /**
40
     * @var string
41
     *
42
     * @validate NotEmpty
43
     * @validate Romm.ConfigurationObject:ClassImplements(interface=CuyZ\Notiz\Core\Channel\Channel)
44
     */
45
    protected $className;
46
47
    /**
48
     * @var string
49
     */
50
    protected $label;
51
52
    /**
53
     * @var ChannelSettings
54
     *
55
     * @mixedTypesResolver \CuyZ\Notiz\Core\Definition\Tree\Notification\Channel\Settings\ChannelSettingsResolver
56
     */
57
    protected $settings;
58
59
    /**
60
     * @param string $identifier
61
     */
62
    public function __construct($identifier)
63
    {
64
        $this->identifier = $identifier;
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getIdentifier()
71
    {
72
        return $this->identifier;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getClassName()
79
    {
80
        return $this->className;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getLabel()
87
    {
88
        return LocalizationService::localize($this->label);
89
    }
90
91
    /**
92
     * @return ChannelSettings
93
     */
94
    public function getSettings()
95
    {
96
        return $this->settings;
97
    }
98
99
    /**
100
     * Method called during the definition object construction: it allows
101
     * manipulating the data array before it is actually used to construct the
102
     * object.
103
     *
104
     * We use it to add the settings class name further in the data array so it
105
     * can be fetched later.
106
     *
107
     * @param DataPreProcessor $processor
108
     */
109
    public static function dataPreProcessor(DataPreProcessor $processor)
110
    {
111
        $data = $processor->getData();
112
113
        // Settings must always be set.
114
        if (!is_array($data['settings'])) {
115
            $data['settings'] = [];
116
        }
117
118
        $data['settings'][ChannelSettings::SETTINGS_CLASS_NAME] = ChannelSettings::TYPE_DEFAULT;
119
120
        try {
121
            $data = self::fetchSettingsClassName($data);
122
        } catch (NotizException $exception) {
123
            $error = new Error($exception->getMessage(), $exception->getCode());
124
            $processor->addError($error);
125
        }
126
127
        $processor->setData($data);
128
    }
129
130
    /**
131
     * @param array $data
132
     * @return array
133
     *
134
     * @throws ClassNotFoundException
135
     * @throws InvalidClassException
136
     */
137
    protected static function fetchSettingsClassName(array $data)
138
    {
139
        // @PHP7
140
        $channelClassName = isset($data['className'])
141
            ? $data['className']
142
            : null;
143
144
        if (class_exists($channelClassName)
145
            && in_array(Channel::class, class_implements($channelClassName))
146
        ) {
147
            /** @var Channel $channelClassName */
148
            $settingsClassName = $channelClassName::getSettingsClassName();
149
150
            if (!class_exists($settingsClassName)) {
151
                throw ClassNotFoundException::channelSettingsClassNotFound($settingsClassName);
152
            }
153
154
            if (!in_array(ChannelSettings::class, class_implements($settingsClassName))) {
155
                throw InvalidClassException::channelSettingsMissingInterface($settingsClassName);
156
            }
157
158
            $data['settings'][ChannelSettings::SETTINGS_CLASS_NAME] = $settingsClassName;
159
        }
160
161
        return $data;
162
    }
163
}
164