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

NotificationContainer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

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