Completed
Push — master ( e3732b...aa3be6 )
by Peter
04:06
created

SpecificationQueryHandler::handleSpecification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Query\Specification;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use Happyr\DoctrineSpecification\EntitySpecificationRepositoryInterface;
15
16
class SpecificationQueryHandler
17
{
18
    /**
19
     * @var EntityManagerInterface
20
     */
21
    private $em;
22
23
    /**
24
     * @param EntityManagerInterface $em
25
     */
26 1
    public function __construct(EntityManagerInterface $em)
27
    {
28 1
        $this->em = $em;
29 1
    }
30
31
    /**
32
     * @param SpecificationQuery $query
33
     *
34
     * @return mixed
35
     */
36 1
    public function handleSpecification(SpecificationQuery $query)
37
    {
38
        /* @var $rep EntitySpecificationRepositoryInterface */
39 1
        $rep = $this->em->getRepository($query->entity());
40
41 1
        return $rep->match($query->spec(), $query->modifier());
0 ignored issues
show
Bug introduced by
It seems like $query->modifier() can be null; however, match() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
42
    }
43
}
44