Passed
Push — master ( 6137ca...01151b )
by Kevin
02:21
created

DBALMatchableObjectRepository::matchOne()   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 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Zenstruck\Porpaginas\Doctrine\Repository;
4
5
use Doctrine\DBAL\Query\QueryBuilder;
6
use Zenstruck\Porpaginas\Doctrine\DBAL\Specification\DBALContext;
7
use Zenstruck\Porpaginas\Exception\NotFound;
8
use Zenstruck\Porpaginas\Matchable;
9
use Zenstruck\Porpaginas\Result;
10
use Zenstruck\Porpaginas\Specification\Normalizer;
11
12
/**
13
 * @author Kevin Bond <[email protected]>
14
 */
15
abstract class DBALMatchableObjectRepository extends DBALObjectRepository implements Matchable
16
{
17 34
    final public function match($specification): Result
18
    {
19 34
        return static::createResult($this->qbForSpecification($specification));
20
    }
21
22 3
    final public function matchOne($specification)
23
    {
24 3
        $result = $this->qbForSpecification($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

24
        $result = /** @scrutinizer ignore-deprecated */ $this->qbForSpecification($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...
25 3
            ->setMaxResults(1)
26 3
            ->execute()
27 3
            ->fetch()
28
        ;
29
30 3
        if (!$result) {
31 1
            throw new NotFound(\sprintf('Object from "%s" table not found for given specification.', static::tableName()));
32
        }
33
34 2
        return static::createObject($result);
35
    }
36
37 37
    final protected function qbForSpecification($specification): QueryBuilder
38
    {
39 37
        $qb = $this->qb();
40 37
        $result = $this->specificationNormalizer()->normalize($specification, new DBALContext($qb));
41
42 37
        if ($result) {
43 33
            $qb->where($result);
44
        }
45
46 37
        return $qb;
47
    }
48
49
    /**
50
     * Override to provide your own SpecificationNormalizer implementation.
51
     */
52 37
    protected function specificationNormalizer(): Normalizer
53
    {
54 37
        return DBALContext::defaultNormalizer();
55
    }
56
}
57