ORMMatchableRepository::matchOne()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Zenstruck\Porpaginas\Doctrine\Repository;
4
5
use Doctrine\ORM\QueryBuilder;
6
use Zenstruck\Porpaginas\Doctrine\ORM\Specification\ORMContext;
7
use Zenstruck\Porpaginas\Doctrine\ORMQueryResult;
8
use Zenstruck\Porpaginas\Exception\NotFound;
9
use Zenstruck\Porpaginas\Matchable;
10
use Zenstruck\Porpaginas\Specification\Normalizer;
11
12
/**
13
 * @author Kevin Bond <[email protected]>
14
 */
15
abstract class ORMMatchableRepository extends ORMRepository implements Matchable
16
{
17 34
    final public function match($specification): ORMQueryResult
18
    {
19 34
        return static::createResult($this->qbForSpecification($specification));
20
    }
21
22 3
    final public function matchOne($specification)
23
    {
24 3
        if (!$result = $this->qbForSpecification($specification)->getQuery()->getOneOrNullResult()) {
25 1
            throw new NotFound("{$this->getClassName()} not found for given specification.");
26
        }
27
28 2
        return $result;
29
    }
30
31 37
    final protected function qbForSpecification($specification): QueryBuilder
32
    {
33 37
        $qb = $this->qb('entity');
34 37
        $result = $this->specificationNormalizer()->normalize($specification, new ORMContext($qb, 'entity'));
35
36 37
        if ($result) {
37 33
            $qb->where($result);
38
        }
39
40 37
        return $qb;
41
    }
42
43
    /**
44
     * Override to provide your own SpecificationNormalizer implementation.
45
     */
46 37
    protected function specificationNormalizer(): Normalizer
47
    {
48 37
        return ORMContext::defaultNormalizer();
49
    }
50
}
51