Completed
Push — 7.5 ( f79096...9972f5 )
by
unknown
27:34
created

DoctrineDatabase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the DoctrineDatabase UrlWildcard Gateway class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Legacy\Content\UrlWildcard\Gateway;
10
11
use Doctrine\DBAL\FetchMode;
12
use eZ\Publish\Core\Persistence\Legacy\Content\UrlWildcard\Gateway;
13
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
14
use eZ\Publish\SPI\Persistence\Content\UrlWildcard;
15
16
/**
17
 * UrlWildcard Gateway.
18
 */
19
class DoctrineDatabase extends Gateway
20
{
21
    /**
22
     * 2^30, since PHP_INT_MAX can cause overflows in DB systems, if PHP is run
23
     * on 64 bit systems.
24
     */
25
    const MAX_LIMIT = 1073741824;
26
27
    /**
28
     * Database handler.
29
     *
30
     * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler
31
     * @deprecated Start to use DBAL $connection instead.
32
     */
33
    protected $dbHandler;
34
35
    /**
36
     * Creates a new DoctrineDatabase Section Gateway.
37
     *
38
     * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler
39
     */
40
    public function __construct(DatabaseHandler $dbHandler)
41
    {
42
        $this->dbHandler = $dbHandler;
43
    }
44
45
    /**
46
     * Inserts the given UrlWildcard.
47
     *
48
     * @param \eZ\Publish\SPI\Persistence\Content\UrlWildcard $urlWildcard
49
     *
50
     * @return mixed
51
     */
52
    public function insertUrlWildcard(UrlWildcard $urlWildcard)
53
    {
54
        /** @var $query \eZ\Publish\Core\Persistence\Database\InsertQuery */
55
        $query = $this->dbHandler->createInsertQuery();
56
        $query->insertInto(
57
            $this->dbHandler->quoteTable('ezurlwildcard')
58
        )->set(
59
            $this->dbHandler->quoteColumn('destination_url'),
60
            $query->bindValue(
61
                trim($urlWildcard->destinationUrl, '/ '),
62
                null,
63
                \PDO::PARAM_STR
64
            )
65
        )->set(
66
            $this->dbHandler->quoteColumn('id'),
67
            $this->dbHandler->getAutoIncrementValue('ezurlwildcard', 'id')
68
        )->set(
69
            $this->dbHandler->quoteColumn('source_url'),
70
            $query->bindValue(
71
                trim($urlWildcard->sourceUrl, '/ '),
72
                null,
73
                \PDO::PARAM_STR
74
            )
75
        )->set(
76
            $this->dbHandler->quoteColumn('type'),
77
            $query->bindValue(
78
                $urlWildcard->forward ? 1 : 2,
79
                null,
80
                \PDO::PARAM_INT
81
            )
82
        );
83
84
        $query->prepare()->execute();
85
86
        return $this->dbHandler->lastInsertId(
87
            $this->dbHandler->getSequenceName('ezurlwildcard', 'id')
88
        );
89
    }
90
91
    /**
92
     * Deletes the UrlWildcard with given $id.
93
     *
94
     * @param mixed $id
95
     */
96
    public function deleteUrlWildcard($id)
97
    {
98
        /** @var $query \eZ\Publish\Core\Persistence\Database\DeleteQuery */
99
        $query = $this->dbHandler->createDeleteQuery();
100
        $query->deleteFrom(
101
            $this->dbHandler->quoteTable('ezurlwildcard')
102
        )->where(
103
            $query->expr->eq(
104
                $this->dbHandler->quoteColumn('id'),
105
                $query->bindValue($id, null, \PDO::PARAM_INT)
106
            )
107
        );
108
        $query->prepare()->execute();
109
    }
110
111
    /**
112
     * Loads an array with data about UrlWildcard with $id.
113
     *
114
     * @param mixed $id
115
     *
116
     * @return array
117
     */
118
    public function loadUrlWildcardData($id)
119
    {
120
        /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
121
        $query = $this->dbHandler->createSelectQuery();
122
        $query->select(
123
            '*'
124
        )->from(
125
            $this->dbHandler->quoteTable('ezurlwildcard')
126
        )->where(
127
            $query->expr->eq(
128
                $this->dbHandler->quoteColumn('id'),
129
                $query->bindValue($id, null, \PDO::PARAM_INT)
130
            )
131
        );
132
        $stmt = $query->prepare();
133
        $stmt->execute();
134
135
        return $stmt->fetch(\PDO::FETCH_ASSOC);
136
    }
137
138
    /**
139
     * Loads an array with data about UrlWildcards (paged).
140
     *
141
     * @param mixed $offset
142
     * @param mixed $limit
143
     *
144
     * @return array
145
     */
146
    public function loadUrlWildcardsData($offset = 0, $limit = -1)
147
    {
148
        $limit = $limit === -1 ? self::MAX_LIMIT : $limit;
149
150
        /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
151
        $query = $this->dbHandler->createSelectQuery();
152
        $query->select(
153
            '*'
154
        )->from(
155
            $this->dbHandler->quoteTable('ezurlwildcard')
156
        )->limit(
157
            $limit > 0 ? $limit : self::MAX_LIMIT,
158
            $offset
159
        );
160
161
        $stmt = $query->prepare();
162
        $stmt->execute();
163
164
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
165
    }
166
167
    /**
168
     * Loads the UrlWildcard with given $sourceUrl.
169
     *
170
     * @param string $sourceUrl
171
     *
172
     * @return array
173
     */
174
    public function loadUrlWildcardBySourceUrl(string $sourceUrl): array
175
    {
176
        /** @var \Doctrine\DBAL\Connection $connection */
177
        $connection = $this->dbHandler->getConnection();
178
        $queryBuilder = $connection->createQueryBuilder();
179
        $expr = $queryBuilder->expr();
180
        $queryBuilder->select(
181
            'id',
182
            'destination_url',
183
            'source_url',
184
            'type'
185
        )
186
        ->from('ezurlwildcard')
187
        ->where(
188
            $expr->eq(
189
                'source_url',
190
                $queryBuilder->createNamedParameter($sourceUrl)
191
            )
192
        );
193
194
        $result = $queryBuilder->execute()->fetch(FetchMode::ASSOCIATIVE);
195
196
        return $result ?: [];
197
    }
198
}
199