ORMMatchableRepository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
c 1
b 0
f 1
dl 0
loc 34
ccs 14
cts 14
cp 1
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 0 3 1
A matchOne() 0 7 2
A qbForSpecification() 0 10 2
A specificationNormalizer() 0 3 1
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