Completed
Push — master ( b2a1ff...4b823c )
by Łukasz
19:14
created

DoctrineDatabase::locationSwapped()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 16
rs 9.4285
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\Bookmark\Gateway;
10
11
use Doctrine\DBAL\Connection;
12
use eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway;
13
use eZ\Publish\SPI\Persistence\Bookmark\Bookmark;
14
use PDO;
15
16
class DoctrineDatabase extends Gateway
17
{
18
    const TABLE_BOOKMARKS = 'ezcontentbrowsebookmark';
19
20
    const COLUMN_ID = 'id';
21
    const COLUMN_USER_ID = 'user_id';
22
    const COLUMN_LOCATION_ID = 'node_id';
23
    const COLUMN_NAME = 'name';
24
25
    /**
26
     * @var \Doctrine\DBAL\Connection
27
     */
28
    protected $connection;
29
30
    public function __construct(Connection $connection)
31
    {
32
        $this->connection = $connection;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function insertBookmark(Bookmark $bookmark): int
39
    {
40
        $query = $this->connection->createQueryBuilder();
41
        $query
42
            ->insert(self::TABLE_BOOKMARKS)
43
            ->values([
44
                self::COLUMN_NAME => ':name',
45
                self::COLUMN_USER_ID => ':user_id',
46
                self::COLUMN_LOCATION_ID => ':location_id',
47
            ])
48
            ->setParameter(':name', $bookmark->name, PDO::PARAM_STR)
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\SPI\Persisten...ookmark\Bookmark::$name has been deprecated with message: Property is here purely for BC with 5.x.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
49
            ->setParameter(':user_id', $bookmark->userId, PDO::PARAM_INT)
50
            ->setParameter(':location_id', $bookmark->locationId, PDO::PARAM_INT);
51
52
        $query->execute();
53
54
        return (int) $this->connection->lastInsertId();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function deleteBookmark(int $id): void
61
    {
62
        $query = $this->connection->createQueryBuilder();
63
        $query
64
            ->delete(self::TABLE_BOOKMARKS)
65
            ->where($query->expr()->eq(self::COLUMN_ID, ':id'))
66
            ->setParameter(':id', $id, PDO::PARAM_INT);
67
68
        $query->execute();
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function loadBookmarkDataById(int $id): array
75
    {
76
        $query = $this->connection->createQueryBuilder();
77
        $query
78
            ->select(...$this->getColumns())
79
            ->from(self::TABLE_BOOKMARKS)
80
            ->where($query->expr()->eq(self::COLUMN_ID, ':id'))
81
            ->setParameter(':id', $id, PDO::PARAM_INT);
82
83
        return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function loadBookmarkDataByUserIdAndLocationId(int $userId, array $locationIds): array
90
    {
91
        $query = $this->connection->createQueryBuilder();
92
        $query
93
            ->select(...$this->getColumns())
94
            ->from(self::TABLE_BOOKMARKS)
95
            ->where($query->expr()->andX(
96
                $query->expr()->eq(self::COLUMN_USER_ID, ':user_id'),
97
                $query->expr()->in(self::COLUMN_LOCATION_ID, ':location_id')
98
            ))
99
            ->setParameter(':user_id', $userId, PDO::PARAM_INT)
100
            ->setParameter(':location_id', $locationIds, Connection::PARAM_INT_ARRAY);
101
102
        return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
109
    {
110
        $query = $this->connection->createQueryBuilder();
111
        $query
112
            ->select(...$this->getColumns())
113
            ->from(self::TABLE_BOOKMARKS)
114
            ->where($query->expr()->eq(self::COLUMN_USER_ID, ':user_id'))
115
            ->setFirstResult($offset);
116
117
        if ($limit > 0) {
118
            $query->setMaxResults($limit);
119
        }
120
121
        $query->orderBy(self::COLUMN_ID, 'DESC');
122
        $query->setParameter(':user_id', $userId, PDO::PARAM_INT);
123
124
        return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function countUserBookmarks(int $userId): int
131
    {
132
        $query = $this->connection->createQueryBuilder();
133
        $query
134
            ->select('COUNT(' . self::COLUMN_ID . ')')
135
            ->from(self::TABLE_BOOKMARKS)
136
            ->where($query->expr()->eq(self::COLUMN_USER_ID, ':user_id'))
137
            ->setParameter(':user_id', $userId, PDO::PARAM_INT);
138
139
        return (int) $query->execute()->fetchColumn();
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function locationSwapped(int $location1Id, int $location2Id): void
146
    {
147
        $query = $this->connection->createQueryBuilder();
148
        $query
149
            ->update(self::TABLE_BOOKMARKS)
150
            ->set(self::COLUMN_LOCATION_ID, '(CASE WHEN node_id = :source_id THEN :target_id ELSE :source_id END)')
151
            ->where($query->expr()->orX(
152
                $query->expr()->eq(self::COLUMN_LOCATION_ID, ':source_id'),
153
                $query->expr()->eq(self::COLUMN_LOCATION_ID, ':target_id')
154
            ));
155
156
        $stmt = $this->connection->prepare($query->getSQL());
157
        $stmt->bindValue('source_id', $location1Id, PDO::PARAM_INT);
158
        $stmt->bindValue('target_id', $location2Id, PDO::PARAM_INT);
159
        $stmt->execute();
160
    }
161
162
    private function getColumns(): array
163
    {
164
        return [
165
            self::COLUMN_ID,
166
            self::COLUMN_NAME,
167
            self::COLUMN_USER_ID,
168
            self::COLUMN_LOCATION_ID,
169
        ];
170
    }
171
}
172