Passed
Push — master ( 273203...3cdd43 )
by Kevin
01:45
created

DBALObjectRepository::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 10
ccs 8
cts 8
cp 1
crap 2
1
<?php
2
3
namespace Zenstruck\Porpaginas\Doctrine\Repository;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Query\QueryBuilder;
7
use Zenstruck\Porpaginas\Doctrine\DBALQueryBuilderResult;
8
use Zenstruck\Porpaginas\Exception\NotFound;
9
use Zenstruck\Porpaginas\Factory\FactoryResult;
10
use Zenstruck\Porpaginas\Repository;
11
use Zenstruck\Porpaginas\Result;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
abstract class DBALObjectRepository implements Repository
17
{
18 2
    public function getIterator(): \Traversable
19
    {
20 2
        return static::createResult($this->qb());
21
    }
22
23 1
    public function count(): int
24
    {
25 1
        return \count(static::createResult($this->qb()));
26
    }
27
28
    /**
29
     * @param callable(QueryBuilder $qb) $specification
30
     */
31 2
    public function get(callable $specification): object
32
    {
33 2
        $result = $this->queryBuilderForSpecification($specification)
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\ResultStatement::fetch() has been deprecated: Use fetchNumeric(), fetchAssociative() or fetchOne() instead. ( Ignorable by Annotation )

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

33
        $result = /** @scrutinizer ignore-deprecated */ $this->queryBuilderForSpecification($specification)

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

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

Loading history...
34 2
            ->setMaxResults(1)
35 2
            ->execute()
36 2
            ->fetch()
37
        ;
38
39 2
        if (!$result) {
40 1
            throw new NotFound(\sprintf('Object from "%s" table not found for given specification.', static::tableName()));
41
        }
42
43 1
        return static::createObject($result);
44
    }
45
46
    /**
47
     * @param callable(QueryBuilder $qb) $specification
48
     */
49 1
    public function filter(callable $specification): Result
50
    {
51 1
        return self::createResult($this->queryBuilderForSpecification($specification));
52
    }
53
54
    abstract protected static function createObject(array $data): object;
55
56
    abstract protected static function tableName(): string;
57
58
    abstract protected function connection(): Connection;
59
60 3
    final protected static function createResult(QueryBuilder $qb): Result
61
    {
62 3
        return new FactoryResult(
63
            static function(array $data) {
64 3
                return static::createObject($data);
65 3
            },
66 3
            static::createDBALQueryBuilderResult($qb)
67
        );
68
    }
69
70 3
    protected static function createDBALQueryBuilderResult(QueryBuilder $qb): DBALQueryBuilderResult
71
    {
72 3
        return new DBALQueryBuilderResult($qb);
73
    }
74
75 5
    protected function qb(): QueryBuilder
76
    {
77 5
        return $this->connection()->createQueryBuilder()->select('*')->from(static::tableName());
78
    }
79
80 3
    private function queryBuilderForSpecification(callable $specification): QueryBuilder
81
    {
82 3
        $specification($qb = $this->qb());
83
84 3
        return $qb;
85
    }
86
}
87