NotificationContainer::__construct()   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\Container;
19
20
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
21
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition;
22
use CuyZ\Notiz\Core\Exception\InvalidTypeException;
23
use Generator;
24
25
/**
26
 * This container is used as a proxy for the notification fetching: it will
27
 * check itself that the notification entries returned by a notification
28
 * processor are correct, without having to do it in the public API.
29
 */
30
class NotificationContainer
31
{
32
    /**
33
     * @var NotificationDefinition
34
     */
35
    protected $notificationDefinition;
36
37
    /**
38
     * @param NotificationDefinition $notificationDefinition
39
     */
40
    public function __construct(NotificationDefinition $notificationDefinition)
41
    {
42
        $this->notificationDefinition = $notificationDefinition;
43
    }
44
45
    /**
46
     * Fetches all notifications that are bound to the given event definition.
47
     *
48
     * @param EventDefinition $eventDefinition
49
     * @return Generator
50
     */
51
    public function fetchFromEventDefinition(EventDefinition $eventDefinition): Generator
52
    {
53
        $processor = $this->notificationDefinition->getProcessor();
54
        $notifications = $processor->getNotificationsFromEventDefinition($eventDefinition);
55
56
        return $this->loop($notifications);
57
    }
58
59
    /**
60
     * Fetches the totality of notifications for the definition of this
61
     * container.
62
     *
63
     * @return Generator
64
     */
65
    public function fetchAll(): Generator
66
    {
67
        $processor = $this->notificationDefinition->getProcessor();
68
        $notifications = $processor->getAllNotifications();
69
70
        return $this->loop($notifications);
71
    }
72
73
    /**
74
     * Checks that the notifications that were fetched are all correct
75
     * instances.
76
     *
77
     * @param array $notifications
78
     * @return Generator
79
     *
80
     * @throws InvalidTypeException
81
     */
82
    protected function loop($notifications): Generator
83
    {
84
        if (!is_array($notifications)) {
0 ignored issues
show
introduced by
The condition is_array($notifications) is always true.
Loading history...
85
            throw InvalidTypeException::notificationContainerArrayInvalidType($notifications);
86
        }
87
88
        $notificationClassName = $this->notificationDefinition->getClassName();
89
90
        foreach ($notifications as $key => $notification) {
91
            if (!$notification instanceof $notificationClassName) {
92
                throw InvalidTypeException::notificationContainerEntryInvalidType($key, $notification, $this->notificationDefinition);
93
            }
94
95
            yield $notification;
96
        }
97
    }
98
}
99