ChannelDefinition::getLabel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Core\Definition\Tree\Notification\Channel;
19
20
use CuyZ\Notiz\Core\Channel\Channel;
21
use CuyZ\Notiz\Core\Channel\Settings\ChannelSettings;
22
use CuyZ\Notiz\Core\Definition\Tree\AbstractDefinitionComponent;
23
use CuyZ\Notiz\Core\Exception\ClassNotFoundException;
24
use CuyZ\Notiz\Core\Exception\InvalidClassException;
25
use CuyZ\Notiz\Core\Exception\NotizException;
26
use CuyZ\Notiz\Service\LocalizationService;
27
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
28
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
29
use TYPO3\CMS\Extbase\Error\Error;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Error\Error was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
31
class ChannelDefinition extends AbstractDefinitionComponent implements DataPreProcessorInterface
32
{
33
    /**
34
     * @var string
35
     *
36
     * @validate NotEmpty
37
     */
38
    protected $identifier;
39
40
    /**
41
     * @var string
42
     *
43
     * @validate NotEmpty
44
     * @validate Romm.ConfigurationObject:ClassImplements(interface=CuyZ\Notiz\Core\Channel\Channel)
45
     */
46
    protected $className;
47
48
    /**
49
     * @var string
50
     */
51
    protected $label;
52
53
    /**
54
     * @var ChannelSettings
55
     *
56
     * @mixedTypesResolver \CuyZ\Notiz\Core\Definition\Tree\Notification\Channel\Settings\ChannelSettingsResolver
57
     */
58
    protected $settings;
59
60
    /**
61
     * @param string $identifier
62
     */
63
    public function __construct(string $identifier)
64
    {
65
        $this->identifier = $identifier;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getIdentifier(): string
72
    {
73
        return $this->identifier;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getClassName(): string
80
    {
81
        return $this->className;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getLabel(): string
88
    {
89
        return LocalizationService::localize($this->label);
90
    }
91
92
    /**
93
     * @return ChannelSettings
94
     */
95
    public function getSettings(): ChannelSettings
96
    {
97
        return $this->settings;
98
    }
99
100
    /**
101
     * Method called during the definition object construction: it allows
102
     * manipulating the data array before it is actually used to construct the
103
     * object.
104
     *
105
     * We use it to add the settings class name further in the data array so it
106
     * can be fetched later.
107
     *
108
     * @param DataPreProcessor $processor
109
     */
110
    public static function dataPreProcessor(DataPreProcessor $processor)
111
    {
112
        $data = $processor->getData();
113
114
        // Settings must always be set.
115
        if (!is_array($data['settings'])) {
116
            $data['settings'] = [];
117
        }
118
119
        $data['settings'][ChannelSettings::SETTINGS_CLASS_NAME] = ChannelSettings::TYPE_DEFAULT;
120
121
        try {
122
            $data = self::fetchSettingsClassName($data);
123
        } catch (NotizException $exception) {
124
            $error = new Error($exception->getMessage(), $exception->getCode());
125
            $processor->addError($error);
126
        }
127
128
        $processor->setData($data);
129
    }
130
131
    /**
132
     * @param array $data
133
     * @return array
134
     *
135
     * @throws ClassNotFoundException
136
     * @throws InvalidClassException
137
     */
138
    protected static function fetchSettingsClassName(array $data): array
139
    {
140
        $channelClassName = $data['className'] ?? null;
141
142
        if (class_exists($channelClassName)
0 ignored issues
show
Bug introduced by
It seems like $channelClassName can also be of type null; however, parameter $class of class_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
        if (class_exists(/** @scrutinizer ignore-type */ $channelClassName)
Loading history...
143
            && in_array(Channel::class, class_implements($channelClassName))
144
        ) {
145
            /** @var Channel $channelClassName */
146
            $settingsClassName = $channelClassName::getSettingsClassName();
147
148
            if (!class_exists($settingsClassName)) {
149
                throw ClassNotFoundException::channelSettingsClassNotFound($settingsClassName);
150
            }
151
152
            if (!in_array(ChannelSettings::class, class_implements($settingsClassName))) {
153
                throw InvalidClassException::channelSettingsMissingInterface($settingsClassName);
154
            }
155
156
            $data['settings'][ChannelSettings::SETTINGS_CLASS_NAME] = $settingsClassName;
157
        }
158
159
        return $data;
160
    }
161
}
162