getFromNotification()   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 1
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\Notification\Processor;
19
20
use CuyZ\Notiz\Core\Exception\ClassNotFoundException;
21
use CuyZ\Notiz\Core\Exception\InvalidClassException;
22
use CuyZ\Notiz\Core\Notification\Notification;
23
use CuyZ\Notiz\Service\Traits\ExtendedSelfInstantiateTrait;
24
use TYPO3\CMS\Core\SingletonInterface;
25
use TYPO3\CMS\Extbase\Object\Container\Container;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Object\Container\Container 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
use TYPO3\CMS\Extbase\Object\ObjectManager;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Object\ObjectManager 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...
27
28
class NotificationProcessorFactory implements SingletonInterface
29
{
30
    use ExtendedSelfInstantiateTrait;
31
32
    /**
33
     * @var NotificationProcessor[]
34
     */
35
    protected $processors = [];
36
37
    /**
38
     * @var ObjectManager
39
     */
40
    protected $objectManager;
41
42
    /**
43
     * @var Container
44
     */
45
    protected $objectContainer;
46
47
    /**
48
     * @param ObjectManager $objectManager
49
     * @param Container $objectContainer
50
     */
51
    public function __construct(ObjectManager $objectManager, Container $objectContainer)
52
    {
53
        $this->objectManager = $objectManager;
54
        $this->objectContainer = $objectContainer;
55
    }
56
57
    /**
58
     * @param Notification $notification
59
     * @return NotificationProcessor
60
     */
61
    public function getFromNotification(Notification $notification): NotificationProcessor
62
    {
63
        return $this->getFromNotificationClassName(get_class($notification));
64
    }
65
66
    /**
67
     * @param string $className
68
     * @return NotificationProcessor
69
     *
70
     * @throws ClassNotFoundException
71
     * @throws InvalidClassException
72
     */
73
    public function getFromNotificationClassName(string $className): NotificationProcessor
74
    {
75
        $className = $this->objectContainer->getImplementationClassName($className);
76
77
        if (false === isset($this->processors[$className])) {
78
            if (!class_exists($className)) {
79
                throw ClassNotFoundException::notificationClassNotFound($className);
80
            }
81
82
            if (!in_array(Notification::class, class_implements($className))) {
83
                throw InvalidClassException::notificationMissingInterface($className);
84
            }
85
86
            $processorClassName = $this->getProcessorClassNameFromNotificationClassName($className);
87
88
            $this->processors[$className] = $this->objectManager->get($processorClassName, $className);
89
        }
90
91
        return $this->processors[$className];
92
    }
93
94
    /**
95
     * @param string $notificationClassName
96
     * @return string
97
     *
98
     * @throws ClassNotFoundException
99
     * @throws InvalidClassException
100
     */
101
    protected function getProcessorClassNameFromNotificationClassName(string $notificationClassName): string
102
    {
103
        /** @var Notification $notificationClassName */
104
        $processorClassName = $notificationClassName::getProcessorClassName();
105
106
        if (!class_exists($processorClassName)) {
107
            throw ClassNotFoundException::notificationProcessorClassNotFound($notificationClassName, $processorClassName);
0 ignored issues
show
Bug introduced by
$notificationClassName of type CuyZ\Notiz\Core\Notification\Notification is incompatible with the type string expected by parameter $notificationClassName of CuyZ\Notiz\Core\Exceptio...rocessorClassNotFound(). ( Ignorable by Annotation )

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

107
            throw ClassNotFoundException::notificationProcessorClassNotFound(/** @scrutinizer ignore-type */ $notificationClassName, $processorClassName);
Loading history...
108
        }
109
110
        if (!in_array(NotificationProcessor::class, class_parents($processorClassName))) {
111
            throw InvalidClassException::notificationProcessorWrongParent($notificationClassName, $processorClassName);
0 ignored issues
show
Bug introduced by
$notificationClassName of type CuyZ\Notiz\Core\Notification\Notification is incompatible with the type string expected by parameter $notificationClassName of CuyZ\Notiz\Core\Exceptio...nProcessorWrongParent(). ( Ignorable by Annotation )

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

111
            throw InvalidClassException::notificationProcessorWrongParent(/** @scrutinizer ignore-type */ $notificationClassName, $processorClassName);
Loading history...
112
        }
113
114
        return $processorClassName;
115
    }
116
}
117