Completed
Push — ezp-30616 ( 0a36b6 )
by
unknown
21:15 queued 06:15
created

NotificationService::markNotificationAsRead()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Event;
10
11
use eZ\Publish\SPI\Repository\Decorator\NotificationServiceDecorator;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use eZ\Publish\API\Repository\NotificationService as NotificationServiceInterface;
14
use eZ\Publish\API\Repository\Values\Notification\CreateStruct;
15
use eZ\Publish\API\Repository\Values\Notification\Notification;
16
use eZ\Publish\Core\Event\Notification\BeforeCreateNotificationEvent;
17
use eZ\Publish\Core\Event\Notification\BeforeDeleteNotificationEvent;
18
use eZ\Publish\Core\Event\Notification\BeforeMarkNotificationAsReadEvent;
19
use eZ\Publish\Core\Event\Notification\CreateNotificationEvent;
20
use eZ\Publish\Core\Event\Notification\DeleteNotificationEvent;
21
use eZ\Publish\Core\Event\Notification\MarkNotificationAsReadEvent;
22
use eZ\Publish\Core\Event\Notification\NotificationEvents;
23
24
class NotificationService extends NotificationServiceDecorator implements NotificationServiceInterface
25
{
26
    /**
27
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
28
     */
29
    protected $eventDispatcher;
30
31
    public function __construct(
32
        NotificationServiceInterface $innerService,
33
        EventDispatcherInterface $eventDispatcher
34
    ) {
35
        parent::__construct($innerService);
36
37
        $this->eventDispatcher = $eventDispatcher;
38
    }
39
40
    public function markNotificationAsRead(Notification $notification): void
41
    {
42
        $eventData = [$notification];
43
44
        $beforeEvent = new BeforeMarkNotificationAsReadEvent(...$eventData);
45
        if ($this->eventDispatcher->dispatch(NotificationEvents::BEFORE_MARK_NOTIFICATION_AS_READ, $beforeEvent)->isPropagationStopped()) {
46
            return;
47
        } else {
48
            parent::markNotificationAsRead($notification);
49
        }
50
51
        $this->eventDispatcher->dispatch(
52
            NotificationEvents::MARK_NOTIFICATION_AS_READ,
53
            new MarkNotificationAsReadEvent(...$eventData)
54
        );
55
    }
56
57 View Code Duplication
    public function createNotification(CreateStruct $createStruct): Notification
58
    {
59
        $eventData = [$createStruct];
60
61
        $beforeEvent = new BeforeCreateNotificationEvent(...$eventData);
62
        if ($this->eventDispatcher->dispatch(NotificationEvents::BEFORE_CREATE_NOTIFICATION, $beforeEvent)->isPropagationStopped()) {
63
            return $beforeEvent->getReturnValue();
64
        } else {
65
            $notification = parent::createNotification($createStruct);
66
        }
67
68
        $this->eventDispatcher->dispatch(
69
            NotificationEvents::CREATE_NOTIFICATION,
70
            new CreateNotificationEvent($notification, ...$eventData)
71
        );
72
73
        return $notification;
74
    }
75
76
    public function deleteNotification(Notification $notification): void
77
    {
78
        $eventData = [$notification];
79
80
        $beforeEvent = new BeforeDeleteNotificationEvent(...$eventData);
81
        if ($this->eventDispatcher->dispatch(NotificationEvents::BEFORE_DELETE_NOTIFICATION, $beforeEvent)->isPropagationStopped()) {
82
            return;
83
        } else {
84
            parent::deleteNotification($notification);
85
        }
86
87
        $this->eventDispatcher->dispatch(
88
            NotificationEvents::DELETE_NOTIFICATION,
89
            new DeleteNotificationEvent(...$eventData)
90
        );
91
    }
92
}
93