Completed
Push — master ( 0837c2...4476a6 )
by Peter
03:45
created

SpecificationQueryHandler::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
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\Handler;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use GpsLab\Component\Query\Specification\SpecificationQuery;
15
use Happyr\DoctrineSpecification\EntitySpecificationRepositoryInterface;
16
use GpsLab\Component\Query\Handler\QueryHandler;
17
use GpsLab\Component\Query\Query;
18
19
class SpecificationQueryHandler implements QueryHandler
20
{
21
    /**
22
     * @var EntityManagerInterface
23
     */
24
    private $em;
25
26
    /**
27
     * @param EntityManagerInterface $em
28
     */
29
    public function __construct(EntityManagerInterface $em)
30
    {
31
        $this->em = $em;
32
    }
33
34
    /**
35
     * @param Query $query
36
     *
37
     * @return mixed
38
     */
39
    public function handle(Query $query)
40
    {
41
        if (!($query instanceof SpecificationQuery)) {
42
            return null;
43
        }
44
45
        /* @var $rep EntitySpecificationRepositoryInterface */
46
        $rep = $this->em->getRepository($query->getEntity());
47
48
        return $rep->match($query->getSpec(), $query->getModifier());
0 ignored issues
show
Bug introduced by
It seems like $query->getModifier() 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...
49
    }
50
}
51