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

Mapper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extractNotificationsFromRows() 0 9 2
A createNotificationFromUpdateStruct() 0 7 1
A extractNotificationFromRow() 0 17 3
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\SPI\Persistence\Notification\Notification;
12
use eZ\Publish\SPI\Persistence\Notification\UpdateStruct;
13
use RuntimeException;
14
15
class Mapper
16
{
17
    /**
18
     * Extracts Bookmark objects from $rows.
19
     *
20
     * @param array $rows
21
     *
22
     * @return \eZ\Publish\SPI\Persistence\Notification\Notification[]
23
     */
24
    public function extractNotificationsFromRows(array $rows): array
25
    {
26
        $notifications = [];
27
        foreach ($rows as $row) {
28
            $notifications[] = $this->extractNotificationFromRow($row);
29
        }
30
31
        return $notifications;
32
    }
33
34
    /**
35
     * @param \eZ\Publish\SPI\Persistence\Notification\UpdateStruct $updateStruct
36
     *
37
     * @return \eZ\Publish\SPI\Persistence\Notification\Notification
38
     */
39
    public function createNotificationFromUpdateStruct(UpdateStruct $updateStruct): Notification
40
    {
41
        $notification = new Notification();
42
        $notification->isPending = $updateStruct->isPending;
43
44
        return $notification;
45
    }
46
47
    /**
48
     * Extract Bookmark object from $row.
49
     *
50
     * @param array $row
51
     *
52
     * @return \eZ\Publish\SPI\Persistence\Notification\Notification
53
     */
54
    private function extractNotificationFromRow(array $row): Notification
55
    {
56
        $notification = new Notification();
57
        $notification->id = (int)$row['id'];
58
        $notification->ownerId = (int)$row['owner_id'];
59
        $notification->type = $row['type'];
60
        $notification->created = (int)$row['created'];
61
        $notification->isPending = (bool) $row['is_pending'];
62
        if ($row['data'] !== null) {
63
            $notification->data = json_decode($row['data'], true);
64
            if (json_last_error() !== JSON_ERROR_NONE) {
65
                throw new RuntimeException('Error while decoding notification data: ' . json_last_error_msg());
66
            }
67
        }
68
69
        return $notification;
70
    }
71
}
72