Completed
Push — master ( 7a82a2...eef030 )
by Pavel
02:12
created

RestRepository::em()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest;
2
3
use Doctrine\ORM\EntityNotFoundException;
4
use Doctrine\ORM\EntityRepository;
5
use Pz\Doctrine\Rest\Contracts\JsonApiResource;
6
7
class RestRepository extends EntityRepository
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $rootAlias;
13
14
    /**
15
     * @return \Doctrine\ORM\EntityManager
16
     */
17
    public function em()
18
    {
19
        return $this->getEntityManager();
20
    }
21
22
    /**
23
     * Base root alias for queries.
24
     *
25
     * @return string
26
     */
27 2
    public function alias()
28
    {
29 2
        if ($this->rootAlias === null) {
30 2
            $reflectionClass = $this->getClassMetadata()->getReflectionClass();
31 2
            if ($reflectionClass->implementsInterface(JsonApiResource::class)) {
32
                $this->rootAlias = call_user_func($reflectionClass->getName(). '::getResourceKey');
33
            } else {
34
                // Camel case to underscore-case
35 2
                $this->rootAlias = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $reflectionClass->getShortName()));
36
            }
37
        }
38
39 2
        return $this->rootAlias;
40
    }
41
42
    /**
43
     * @param RestRequestAbstract $request
44
     *
45
     * @return null|object
46
     * @throws EntityNotFoundException
47
     */
48
    public function findByIdentifier(RestRequestAbstract $request)
49
    {
50
        if (null === ($entity = $this->find($request->getId()))) {
51
            throw new EntityNotFoundException($this->getClassName(), $request->getId());
0 ignored issues
show
Bug introduced by
It seems like $request->getId() can also be of type array; however, parameter $code of Doctrine\ORM\EntityNotFo...xception::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            throw new EntityNotFoundException($this->getClassName(), /** @scrutinizer ignore-type */ $request->getId());
Loading history...
52
        }
53
54
        return $entity;
55
    }
56
}
57