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

WalkableDbalQuery::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
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\DbalWalkablePaginator;
9
use ReadModel\Paginator;
10
use ReadModel\Walker\ResultWalker;
11
use ReadModel\Walker\WalkerBuilder;
12
13
abstract class WalkableDbalQuery extends DbalQuery
14
{
15
    /** @var WalkerBuilder */
16
    private $walkerBuilder;
17
18
    /** @var array */
19
    protected $typeMapping = [];
20
21
    public function __construct(Connection $connection, WalkerBuilder $walkerBuilder)
22
    {
23
        parent::__construct($connection);
24
        $this->walkerBuilder = $walkerBuilder;
25
    }
26
27
    protected function createPaginator(QueryBuilder $qb, int $limit = null, $offset = 0, string ...$parameters): Paginator
28
    {
29
        $limit = $limit ?? $this->defaultLimit;
30
        $this->transformParameters($qb, ...$parameters);
31
32
        return new DbalWalkablePaginator($qb, $limit, $offset, $this->buildWalker($parameters));
33
    }
34
35
    protected function getResult(QueryBuilder $qb, string ...$parameters): array
36
    {
37
        $walker = $this->buildWalker($parameters);
38
39
        return array_map(function ($result) use ($walker) {
40
            return $walker->walk($result);
41
        }, parent::getResult($qb, ...$parameters));
42
    }
43
44
    protected function getSingleResult(QueryBuilder $qb, string ...$parameters): array
45
    {
46
        return $this->buildWalker($parameters)->walk(
47
            parent::getSingleResult($qb, ...$parameters)
48
        );
49
    }
50
51
    protected function embed(...$prefixes): void
52
    {
53
        $this->walkerBuilder->withEmbedded(...$prefixes);
54
    }
55
56
    protected function add(callable $callable): void
57
    {
58
        $this->walkerBuilder->with($callable);
59
    }
60
61
    protected function createWalkerBuilder(array $uuids): WalkerBuilder
0 ignored issues
show
Unused Code introduced by
The parameter $uuids 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

61
    protected function createWalkerBuilder(/** @scrutinizer ignore-unused */ array $uuids): WalkerBuilder

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...
62
    {
63
        return $this->walkerBuilder
64
            ->withCamelCasedFieldNames()
65
            ->withScalarCasting($this->typeMapping);
66
    }
67
68
    protected function buildWalker(array $uuids): ResultWalker
69
    {
70
        return $this->createWalkerBuilder($uuids)->build();
71
    }
72
}
73