DefinitionValidator::isValid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
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\Validation\Validator;
19
20
use CuyZ\Notiz\Core\Channel\Channel;
21
use CuyZ\Notiz\Core\Definition\Tree\Definition;
22
use CuyZ\Notiz\Core\Exception\InvalidTypeException;
23
use CuyZ\Notiz\Core\Exception\NotizException;
24
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...
25
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Valida...dator\AbstractValidator 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...
26
27
/**
28
 * Will perform several validation operations across the whole definition tree.
29
 */
30
class DefinitionValidator extends AbstractValidator
31
{
32
    const CHANNEL_NOTIFICATION_NOT_SUPPORTED = 'The channel `%s` does not support notifications of type `%s`.';
33
34
    /**
35
     * @var Definition
36
     */
37
    protected $definition;
38
39
    /**
40
     * @param Definition $definition
41
     *
42
     * @throws InvalidTypeException
43
     */
44
    protected function isValid($definition)
45
    {
46
        if (!$definition instanceof Definition) {
0 ignored issues
show
introduced by
$definition is always a sub-type of CuyZ\Notiz\Core\Definition\Tree\Definition.
Loading history...
47
            throw InvalidTypeException::definitionValidationWrongType($definition);
48
        }
49
50
        $this->definition = $definition;
51
52
        $this->channelsSupportNotifications();
53
    }
54
55
    /**
56
     * Checks that the channels used by the notifications actually support them.
57
     */
58
    protected function channelsSupportNotifications()
59
    {
60
        foreach ($this->definition->getNotifications() as $notification) {
61
            foreach ($notification->getChannels() as $channelDefinition) {
62
                $path = 'notifications.' . $notification->getIdentifier() . '.channels.' . $channelDefinition->getIdentifier();
63
64
                /** @var Channel $channelClassName */
65
                $channelClassName = $channelDefinition->getClassName();
66
67
                try {
68
                    $flag = $channelClassName::supportsNotification($notification);
69
70
                    if (false === $flag) {
71
                        $this->addPropertyError(
72
                            $path,
73
                            self::CHANNEL_NOTIFICATION_NOT_SUPPORTED,
74
                            1506449217,
75
                            [$channelClassName, $notification->getClassName()]
76
                        );
77
                    }
78
                } catch (NotizException $exception) {
79
                    $this->addPropertyError(
80
                        $path,
81
                        $exception->getMessage(),
82
                        $exception->getCode()
83
                    );
84
                }
85
            }
86
        }
87
    }
88
89
    /**
90
     * @param string $path
91
     * @param string $message
92
     * @param int $code
93
     * @param array $arguments
94
     */
95
    protected function addPropertyError(string $path, string $message, int $code, array $arguments = [])
96
    {
97
        $error = new Error($message, $code, $arguments);
98
        $this->result->forProperty($path)->addError($error);
99
    }
100
}
101