Passed
Push — master ( 4595aa...20e07b )
by Romain
06:34
created

getFromNotification()   A

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

106
            throw ClassNotFoundException::notificationProcessorClassNotFound(/** @scrutinizer ignore-type */ $notificationClassName, $processorClassName);
Loading history...
107
        }
108
109
        if (!in_array(NotificationProcessor::class, class_parents($processorClassName))) {
110
            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

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