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

DBALObjectRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 19
c 3
b 0
f 1
dl 0
loc 69
rs 10
ccs 26
cts 26
cp 1
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A queryBuilderForSpecification() 0 5 1
A count() 0 3 1
A qb() 0 3 1
A createResult() 0 7 1
A get() 0 13 2
A getIterator() 0 3 1
A filter() 0 3 1
A createDBALQueryBuilderResult() 0 3 1
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