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

EventFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 16
rs 9.4285
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\Event\Service;
18
19
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
20
use CuyZ\Notiz\Core\Event\Event;
21
use CuyZ\Notiz\Core\Exception\ClassNotFoundException;
22
use CuyZ\Notiz\Core\Exception\InvalidClassException;
23
use CuyZ\Notiz\Core\Notification\Notification;
24
use TYPO3\CMS\Core\SingletonInterface;
25
use TYPO3\CMS\Extbase\Object\ObjectManager;
26
27
class EventFactory implements SingletonInterface
28
{
29
    /**
30
     * @var EventRegistry
31
     */
32
    protected $eventRegistry;
33
34
    /**
35
     * @var ObjectManager
36
     */
37
    protected $objectManager;
38
39
    /**
40
     * @param EventRegistry $eventRegistry
41
     * @param ObjectManager $objectManager
42
     */
43
    public function __construct(EventRegistry $eventRegistry, ObjectManager $objectManager)
44
    {
45
        $this->eventRegistry = $eventRegistry;
46
        $this->objectManager = $objectManager;
47
    }
48
49
    /**
50
     * @param EventDefinition $eventDefinition
51
     * @param Notification $notification
52
     * @return Event
53
     *
54
     * @throws ClassNotFoundException
55
     * @throws InvalidClassException
56
     */
57
    public function create(EventDefinition $eventDefinition, Notification $notification)
58
    {
59
        $className = $eventDefinition->getClassName();
60
61
        if (!class_exists($className)) {
62
            throw ClassNotFoundException::eventClassNotFound($className);
63
        }
64
65
        if (!in_array(Event::class, class_implements($className))) {
66
            throw InvalidClassException::eventHasMissingInterface($className);
67
        }
68
69
        /** @var Event $event */
70
        $event = $this->objectManager->get($className, $eventDefinition, $notification->getEventConfiguration($eventDefinition));
71
72
        return $event;
73
    }
74
}
75