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

Handler::updateNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 11
rs 9.9
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\Legacy\Notification;
10
11
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
12
use eZ\Publish\SPI\Persistence\Notification\CreateStruct;
13
use eZ\Publish\SPI\Persistence\Notification\Handler as HandlerInterface;
14
use eZ\Publish\SPI\Persistence\Notification\Notification;
15
use eZ\Publish\SPI\Persistence\Notification\UpdateStruct;
16
use eZ\Publish\API\Repository\Values\Notification\Notification as APINotification;
17
18
class Handler implements HandlerInterface
19
{
20
    /** @var \eZ\Publish\Core\Persistence\Legacy\Notification\Gateway */
21
    protected $gateway;
22
23
    /** @var \eZ\Publish\Core\Persistence\Legacy\Notification\Mapper */
24
    protected $mapper;
25
26
    /**
27
     * @param \eZ\Publish\Core\Persistence\Legacy\Notification\Gateway $gateway
28
     * @param \eZ\Publish\Core\Persistence\Legacy\Notification\Mapper $mapper
29
     */
30
    public function __construct(Gateway $gateway, Mapper $mapper)
31
    {
32
        $this->gateway = $gateway;
33
        $this->mapper = $mapper;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     *
39
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
40
     */
41
    public function createNotification(CreateStruct $createStruct): Notification
42
    {
43
        $id = $this->gateway->insert($createStruct);
44
45
        return $this->getNotificationById($id);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function countPendingNotifications(int $ownerId): int
52
    {
53
        return $this->gateway->countUserPendingNotifications($ownerId);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     *
59
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
60
     */
61
    public function getNotificationById(int $notificationId): Notification
62
    {
63
        $notification = $this->mapper->extractNotificationsFromRows(
64
            $this->gateway->getNotificationById($notificationId)
65
        );
66
67
        if (count($notification) < 1) {
68
            throw new NotFoundException('Notification', $notificationId);
69
        }
70
71
        return reset($notification);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
78
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
79
     */
80
    public function updateNotification(APINotification $apiNotification, UpdateStruct $updateStruct): Notification
81
    {
82
        $notification = $this->mapper->createNotificationFromUpdateStruct(
83
            $updateStruct
84
        );
85
        $notification->id = $apiNotification->id;
86
87
        $this->gateway->updateNotification($notification);
88
89
        return $this->getNotificationById($notification->id);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function countNotifications(int $userId): int
96
    {
97
        return $this->gateway->countUserNotifications($userId);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function loadUserNotifications(int $userId, int $offset, int $limit): array
104
    {
105
        return $this->mapper->extractNotificationsFromRows(
106
            $this->gateway->loadUserNotifications($userId, $offset, $limit)
107
        );
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function delete(APINotification $notification): void
114
    {
115
        $this->gateway->delete($notification->id);
116
    }
117
}
118