Completed
Push — master ( 7d9afb...4e0ac6 )
by Łukasz
49:46 queued 21:48
created

NotificationService::getNotificationCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
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\Repository;
10
11
use DateTime;
12
use eZ\Publish\API\Repository\NotificationService as NotificationServiceInterface;
13
use eZ\Publish\API\Repository\PermissionResolver;
14
use eZ\Publish\API\Repository\Values\Notification\CreateStruct as APICreateStruct;
15
use eZ\Publish\API\Repository\Values\Notification\Notification as APINotification;
16
use eZ\Publish\API\Repository\Values\Notification\NotificationList;
17
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
18
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
19
use eZ\Publish\SPI\Persistence\Notification\CreateStruct;
20
use eZ\Publish\SPI\Persistence\Notification\Handler;
21
use eZ\Publish\SPI\Persistence\Notification\Notification;
22
use eZ\Publish\SPI\Persistence\Notification\UpdateStruct;
23
24
class NotificationService implements NotificationServiceInterface
25
{
26
    /** @var \eZ\Publish\SPI\Persistence\Notification\Handler $persistenceHandler */
27
    protected $persistenceHandler;
28
29
    /** @var \eZ\Publish\API\Repository\PermissionResolver $permissionResolver */
30
    protected $permissionResolver;
31
32
    /**
33
     * @param \eZ\Publish\SPI\Persistence\Notification\Handler $persistenceHandler
34
     * @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver
35
     */
36
    public function __construct(Handler $persistenceHandler, PermissionResolver $permissionResolver)
37
    {
38
        $this->persistenceHandler = $persistenceHandler;
39
        $this->permissionResolver = $permissionResolver;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function loadNotifications(int $offset = 0, int $limit = 25): NotificationList
46
    {
47
        $currentUserId = $this->getCurrentUserId();
48
49
        $list = new NotificationList();
50
        $list->totalCount = $this->persistenceHandler->countNotifications($currentUserId);
51
        if ($list->totalCount > 0) {
52
            $list->items = array_map(function (Notification $spiNotification) {
53
                return $this->buildDomainObject($spiNotification);
54
            }, $this->persistenceHandler->loadUserNotifications($currentUserId, $offset, $limit));
55
        }
56
57
        return $list;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
64
     */
65
    public function getNotification(int $notificationId): APINotification
66
    {
67
        $currentUserId = $this->getCurrentUserId();
68
69
        $notification = $this->persistenceHandler->getNotificationById($notificationId);
70
71
        if (!$notification->ownerId || $currentUserId != $notification->ownerId) {
72
            throw new NotFoundException('Notification', $notificationId);
73
        }
74
75
        return $this->buildDomainObject($notification);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
82
     * @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException
83
     */
84
    public function markNotificationAsRead(APINotification $notification): void
85
    {
86
        $currentUserId = $this->getCurrentUserId();
87
88
        if (!$notification->id) {
89
            throw new NotFoundException('Notification', $notification->id);
90
        }
91
92
        if ($notification->ownerId !== $currentUserId) {
93
            throw new UnauthorizedException($notification->id, 'Notification');
94
        }
95
96
        if (!$notification->isPending) {
97
            return;
98
        }
99
100
        $updateStruct = new UpdateStruct();
101
102
        $updateStruct->isPending = false;
103
104
        $this->persistenceHandler->updateNotification($notification, $updateStruct);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getPendingNotificationCount(): int
111
    {
112
        $currentUserId = $this->getCurrentUserId();
113
114
        return $this->persistenceHandler->countPendingNotifications($currentUserId);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getNotificationCount(): int
121
    {
122
        $currentUserId = $this->getCurrentUserId();
123
124
        return $this->persistenceHandler->countNotifications($currentUserId);
125
    }
126
127
    /**
128
     * @return int
129
     */
130
    private function getCurrentUserId(): int
131
    {
132
        return $this->permissionResolver
133
            ->getCurrentUserReference()
134
            ->getUserId();
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function deleteNotification(APINotification $notification): void
141
    {
142
        $this->persistenceHandler->delete($notification);
143
    }
144
145
    /**
146
     * @param \eZ\Publish\SPI\Persistence\Notification\Notification $spiNotification
147
     *
148
     * @return \eZ\Publish\API\Repository\Values\Notification\Notification
149
     */
150
    protected function buildDomainObject(Notification $spiNotification): APINotification
151
    {
152
        return new APINotification([
153
            'id' => $spiNotification->id,
154
            'ownerId' => $spiNotification->ownerId,
155
            'isPending' => $spiNotification->isPending,
156
            'type' => $spiNotification->type,
157
            'created' => new DateTime("@{$spiNotification->created}"),
158
            'data' => $spiNotification->data,
159
        ]);
160
    }
161
162
    /**
163
     * @param \eZ\Publish\API\Repository\Values\Notification\CreateStruct $createStruct
164
     *
165
     * @return \eZ\Publish\API\Repository\Values\Notification\Notification
166
     */
167
    public function createNotification(APICreateStruct $createStruct): APINotification
168
    {
169
        $spiCreateStruct = new CreateStruct();
170
        $spiCreateStruct->ownerId = $createStruct->ownerId;
171
        $spiCreateStruct->type = $createStruct->type;
172
        $spiCreateStruct->isPending = $createStruct->isPending;
173
        $spiCreateStruct->data = $createStruct->data;
174
        $spiCreateStruct->created = (new DateTime())->getTimestamp();
175
176
        return $this->buildDomainObject(
177
            $this->persistenceHandler->createNotification($spiCreateStruct)
178
        );
179
    }
180
}
181