Completed
Push — master ( 2e9837...395099 )
by
unknown
143:54 queued 131:18
created

DoctrineDatabase::buildLoadUrlWildcardDataQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
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\Content\UrlWildcard\Gateway;
10
11
use Doctrine\DBAL\Connection;
12
use Doctrine\DBAL\FetchMode;
13
use Doctrine\DBAL\ParameterType;
14
use Doctrine\DBAL\Query\QueryBuilder;
15
use eZ\Publish\Core\Persistence\Legacy\Content\UrlWildcard\Gateway;
16
use eZ\Publish\SPI\Persistence\Content\UrlWildcard;
17
18
/**
19
 * URL wildcard gateway implementation using the Doctrine database.
20
 *
21
 * @internal Gateway implementation is considered internal. Use Persistence UrlWildcard Handler instead.
22
 *
23
 * @see \eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler
24
 */
25
final class DoctrineDatabase extends Gateway
26
{
27
    /**
28
     * 2^30, since PHP_INT_MAX can cause overflows in DB systems, if PHP is run
29
     * on 64 bit systems.
30
     */
31
    private const MAX_LIMIT = 1073741824;
32
33
    /** @var \Doctrine\DBAL\Connection */
34
    private $connection;
35
36
    public function __construct(Connection $connection)
37
    {
38
        $this->connection = $connection;
39
    }
40
41
    public function insertUrlWildcard(UrlWildcard $urlWildcard): int
42
    {
43
        $query = $this->connection->createQueryBuilder();
44
        $query
45
            ->insert(self::URL_WILDCARD_TABLE)
46
            ->values(
47
                [
48
                    'destination_url' => $query->createPositionalParameter(
49
                        trim($urlWildcard->destinationUrl, '/ '),
50
                        ParameterType::STRING
51
                    ),
52
                    'source_url' => $query->createPositionalParameter(
53
                        trim($urlWildcard->sourceUrl, '/ '),
54
                        ParameterType::STRING
55
                    ),
56
                    'type' => $query->createPositionalParameter(
57
                        $urlWildcard->forward ? 1 : 2,
58
                        ParameterType::INTEGER
59
                    ),
60
                ]
61
            );
62
63
        $query->execute();
64
65
        return (int)$this->connection->lastInsertId(self::URL_WILDCARD_SEQ);
66
    }
67
68
    public function deleteUrlWildcard(int $id): void
69
    {
70
        $query = $this->connection->createQueryBuilder();
71
        $query
72
            ->delete(self::URL_WILDCARD_TABLE)
73
            ->where(
74
                $query->expr()->eq(
75
                    'id',
76
                    $query->createPositionalParameter($id, ParameterType::INTEGER)
77
                )
78
            );
79
        $query->execute();
80
    }
81
82
    private function buildLoadUrlWildcardDataQuery(): QueryBuilder
83
    {
84
        $query = $this->connection->createQueryBuilder();
85
        $query
86
            ->select('id', 'destination_url', 'source_url', 'type')
87
            ->from(self::URL_WILDCARD_TABLE);
88
89
        return $query;
90
    }
91
92
    public function loadUrlWildcardData(int $id): array
93
    {
94
        $query = $this->buildLoadUrlWildcardDataQuery();
95
        $query
96
            ->where(
97
                $query->expr()->eq(
98
                    'id',
99
                    $query->createPositionalParameter($id, ParameterType::INTEGER)
100
                )
101
            );
102
        $result = $query->execute()->fetch(FetchMode::ASSOCIATIVE);
103
104
        return false !== $result ? $result : [];
105
    }
106
107
    public function loadUrlWildcardsData(int $offset = 0, int $limit = -1): array
108
    {
109
        $query = $this->buildLoadUrlWildcardDataQuery();
110
        $query
111
            ->setMaxResults($limit > 0 ? $limit : self::MAX_LIMIT)
112
            ->setFirstResult($offset);
113
114
        $stmt = $query->execute();
115
116
        return $stmt->fetchAll(FetchMode::ASSOCIATIVE);
117
    }
118
119
    public function loadUrlWildcardBySourceUrl(string $sourceUrl): array
120
    {
121
        $query = $this->buildLoadUrlWildcardDataQuery();
122
        $expr = $query->expr();
123
        $query
124
            ->where(
125
                $expr->eq(
126
                    'source_url',
127
                    $query->createPositionalParameter($sourceUrl)
128
                )
129
            );
130
131
        $result = $query->execute()->fetch(FetchMode::ASSOCIATIVE);
132
133
        return false !== $result ? $result : [];
134
    }
135
}
136