Passed
Push — master ( 82a58b...d35cf8 )
by Tomasz
02:32
created

DbalQuery::getSingleResult()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 1
nop 2
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace ReadModel\Bridge\Doctrine\Query;
5
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\Query\QueryBuilder;
8
use ReadModel\Bridge\Doctrine\ApplyFilters;
9
use ReadModel\Bridge\Doctrine\DbalPaginator;
10
use ReadModel\Filters\Filters;
11
use ReadModel\NotFoundException;
12
use ReadModel\Paginator;
13
14
abstract class DbalQuery
15
{
16
    /** @var Connection */
17
    protected $connection;
18
19
    /** @var int */
20
    protected $defaultLimit = 100;
21
22
    /** @var array */
23
    protected $columnMapping = [];
24
25
    public function __construct(Connection $connection)
26
    {
27
        $this->connection = $connection;
28
    }
29
30
    protected function createQueryBuilder(): QueryBuilder
31
    {
32
        return $this->connection->createQueryBuilder();
33
    }
34
35
    protected function createPaginator(QueryBuilder $qb, int $limit = null, $offset = 0, string ...$parameters): Paginator
36
    {
37
        $limit = $limit ?? $this->defaultLimit;
38
        $this->transformParameters($qb, ...$parameters);
39
40
        return new DbalPaginator($qb, $limit, $offset);
41
    }
42
43
    protected function createPaginatorForFilters(QueryBuilder $qb, Filters $filters, string ...$parameters): Paginator
44
    {
45
        $paginator = $this->createPaginator($qb, $filters->limit(), $filters->offset(), ...$parameters);
46
        $filters->addMetaToPaginator($paginator);
47
48
        return $paginator;
49
    }
50
51
    protected function applyFilters(QueryBuilder $qb, Filters $filters): void
52
    {
53
        (new ApplyFilters($this->columnMapping))($qb, $filters);
54
    }
55
56
    protected function getResult(QueryBuilder $qb, string ...$parameters): array
57
    {
58
        $this->transformParameters($qb, ...$parameters);
59
60
        return $qb->execute()->fetchAll();
61
    }
62
63
    protected function getScalarResult(QueryBuilder $qb, string ...$parameters): array
64
    {
65
        return array_map(function (array $row) {
66
            return $this->getColumn($row);
67
        }, $this->getResult($qb, ...$parameters));
68
    }
69
70
    protected function getSingleResult(QueryBuilder $qb, string ...$parameters): array
71
    {
72
        $this->transformParameters($qb, ...$parameters);
73
        $qb->setMaxResults(1);
74
        $result = $qb->execute()->fetch();
75
76
        if ($result === false) {
77
            throw new NotFoundException();
78
        }
79
80
        return $result;
81
    }
82
83
    protected function getSingleScalarResult(QueryBuilder $qb, ...$parameters)
84
    {
85
        return $this->getColumn(
86
            $this->getSingleResult($qb, ...$parameters)
87
        );
88
    }
89
90
    protected function transformParameters(QueryBuilder $qb, string ...$parameters): void
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

90
    protected function transformParameters(QueryBuilder $qb, /** @scrutinizer ignore-unused */ string ...$parameters): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $qb is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

90
    protected function transformParameters(/** @scrutinizer ignore-unused */ QueryBuilder $qb, string ...$parameters): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
    {
92
        // do nothing, you can override this method or use trait TransformUuidToBytes
93
    }
94
95
    private function getColumn(array $result)
96
    {
97
        if (count($result) > 1) {
98
            throw new TooManyColumnsException(array_keys($result));
99
        }
100
101
        return array_shift($result);
102
    }
103
}
104