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

NotificationHandler::countNotifications()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 21
loc 21
rs 9.584
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\Persistence\Cache;
10
11
use eZ\Publish\SPI\Persistence\Notification\CreateStruct;
12
use eZ\Publish\SPI\Persistence\Notification\Handler;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, eZ\Publish\Core\Persistence\Cache\Handler.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use eZ\Publish\SPI\Persistence\Notification\Notification;
14
use eZ\Publish\SPI\Persistence\Notification\UpdateStruct;
15
use eZ\Publish\API\Repository\Values\Notification\Notification as APINotification;
16
17
/**
18
 * SPI cache for Notification Handler.
19
 *
20
 * @see \eZ\Publish\SPI\Persistence\Notification\Handler
21
 */
22
class NotificationHandler extends AbstractHandler implements Handler
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function createNotification(CreateStruct $createStruct): Notification
28
    {
29
        $this->logger->logCall(__METHOD__, [
30
            'createStruct' => $createStruct,
31
        ]);
32
33
        $this->cache->invalidateTags([
34
            'notification-count-' . $createStruct->ownerId,
35
            'notification-pending-count-' . $createStruct->ownerId,
36
        ]);
37
38
        return $this->persistenceHandler->notificationHandler()->createNotification($createStruct);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 View Code Duplication
    public function updateNotification(APINotification $notification, UpdateStruct $updateStruct): Notification
45
    {
46
        $this->logger->logCall(__METHOD__, [
47
            'notificationId' => $notification->id,
48
        ]);
49
50
        $this->cache->invalidateTags([
51
            'notification-' . $notification->id,
52
            'notification-pending-count-' . $notification->ownerId,
53
        ]);
54
55
        return $this->persistenceHandler->notificationHandler()->updateNotification($notification, $updateStruct);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function delete(APINotification $notification): void
62
    {
63
        $this->logger->logCall(__METHOD__, [
64
            'notificationId' => $notification->id,
65
        ]);
66
67
        $this->cache->invalidateTags([
68
            'notification-' . $notification->id,
69
            'notification-count-' . $notification->ownerId,
70
            'notification-pending-count-' . $notification->ownerId,
71
        ]);
72
73
        $this->persistenceHandler->notificationHandler()->delete($notification);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 View Code Duplication
    public function countPendingNotifications(int $ownerId): int
80
    {
81
        $cacheItem = $this->cache->getItem('ez-notification-pending-count-' . $ownerId);
82
83
        $count = $cacheItem->get();
84
        if ($cacheItem->isHit()) {
85
            return $count;
86
        }
87
88
        $this->logger->logCall(__METHOD__, [
89
            'ownerId' => $ownerId,
90
        ]);
91
92
        $count = $this->persistenceHandler->notificationHandler()->countPendingNotifications($ownerId);
93
94
        $cacheItem->set($count);
95
        $cacheItem->tag(['notification-pending-count-' . $ownerId]);
96
        $this->cache->save($cacheItem);
97
98
        return $count;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 View Code Duplication
    public function countNotifications(int $ownerId): int
105
    {
106
        $cacheItem = $this->cache->getItem('ez-notification-count-' . $ownerId);
107
108
        $count = $cacheItem->get();
109
        if ($cacheItem->isHit()) {
110
            return $count;
111
        }
112
113
        $this->logger->logCall(__METHOD__, [
114
            'ownerId' => $ownerId,
115
        ]);
116
117
        $count = $this->persistenceHandler->notificationHandler()->countNotifications($ownerId);
118
119
        $cacheItem->set($count);
120
        $cacheItem->tag(['notification-count-' . $ownerId]);
121
        $this->cache->save($cacheItem);
122
123
        return $count;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 View Code Duplication
    public function getNotificationById(int $notificationId): Notification
130
    {
131
        $cacheItem = $this->cache->getItem('ez-notification-' . $notificationId);
132
133
        $notification = $cacheItem->get();
134
        if ($cacheItem->isHit()) {
135
            return $notification;
136
        }
137
138
        $this->logger->logCall(__METHOD__, [
139
            'notificationId' => $notificationId,
140
        ]);
141
142
        $notification = $this->persistenceHandler->notificationHandler()->getNotificationById($notificationId);
143
144
        $cacheItem->set($notification);
145
        $cacheItem->tag(['notification-' . $notificationId]);
146
        $this->cache->save($cacheItem);
147
148
        return $notification;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 View Code Duplication
    public function loadUserNotifications(int $userId, int $offset, int $limit): array
155
    {
156
        $this->logger->logCall(__METHOD__, [
157
            'ownerId' => $userId,
158
            'offset' => $offset,
159
            'limit' => $limit,
160
        ]);
161
162
        return $this->persistenceHandler->notificationHandler()->loadUserNotifications($userId, $offset, $limit);
163
    }
164
}
165