Completed
Push — master ( 0342a7...fd0001 )
by Pavel
01:40
created

RestRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A em() 0 3 1
A alias() 0 13 3
A findByIdentifier() 0 10 2
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 4
    public function em()
18
    {
19 4
        return $this->getEntityManager();
20
    }
21
22
    /**
23
     * Base root alias for queries.
24
     *
25
     * @return string
26
     */
27 7
    public function alias()
28
    {
29 7
        if ($this->rootAlias === null) {
30 7
            $reflectionClass = $this->getClassMetadata()->getReflectionClass();
31 7
            if ($reflectionClass->implementsInterface(JsonApiResource::class)) {
32 6
                $this->rootAlias = call_user_func($reflectionClass->getName(). '::getResourceKey');
33
            } else {
34
                // Camel case to underscore-case
35 1
                $this->rootAlias = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $reflectionClass->getShortName()));
36
            }
37
        }
38
39 7
        return $this->rootAlias;
40
    }
41
42
    /**
43
     * @param RestRequest $request
44
     *
45
     * @return null|object
46
     * @throws EntityNotFoundException
47
     */
48 6
    public function findByIdentifier(RestRequest $request)
49
    {
50 6
        if (null === ($entity = $this->find($request->getId()))) {
51 1
            throw EntityNotFoundException::fromClassNameAndIdentifier(
52 1
                $this->getClassName(),
53 1
                ['id' => $request->getId()]
54
            );
55
        }
56
57 5
        return $entity;
58
    }
59
}
60