RestRepository::sourceQueryBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest;
2
3
use Doctrine\ORM\EntityManager;
4
use Doctrine\ORM\EntityRepository;
5
use Pz\Doctrine\Rest\Contracts\JsonApiResource;
6
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
7
use Pz\Doctrine\Rest\Exceptions\RestException;
8
9
class RestRepository extends EntityRepository
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $alias;
15
16
    protected $resourceKey;
17
18
    /**
19
     * @param EntityManager $em
20
     * @param string        $class
21
     *
22
     * @return RestRepository
23
     */
24 14
    public static function create(EntityManager $em, $class)
25
    {
26 14
        return new RestRepository($em, $em->getClassMetadata($class));
27
    }
28
29
    /**
30
     * @return \Doctrine\ORM\EntityManager
31
     */
32 13
    public function getEntityManager()
33
    {
34 13
        return parent::getEntityManager();
35
    }
36
37
    /**
38
     * @param RestRequestContract $request
39
     * @return \Doctrine\ORM\QueryBuilder
40
     */
41 12
    public function sourceQueryBuilder($request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

41
    public function sourceQueryBuilder(/** @scrutinizer ignore-unused */ $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43 12
        return $this->createQueryBuilder($this->alias());
44
    }
45
46
    /**
47
     * @return string|null
48
     */
49 19
    public function getResourceKey()
50
    {
51 19
        if (isset(class_implements($this->getClassName())[JsonApiResource::class])) {
52 18
            return call_user_func("{$this->getClassName()}::getResourceKey");
53
        }
54
55 1
        return null;
56
    }
57
58
    /**
59
     * Base root alias for queries.
60
     *
61
     * @return string
62
     */
63 12
    public function alias()
64
    {
65 12
        if ($this->alias === null) {
66 12
            $this->alias = strtolower(
67
                    preg_replace(
68 12
                    '/(?<!^)[A-Z]/', '_$0',
69 12
                    $this->getClassMetadata()->getReflectionClass()->getShortName()
70
                )
71
            );
72
        }
73
74 12
        return $this->alias;
75
    }
76
77
    /**
78
     * @param mixed $id
79
     *
80
     * @return JsonApiResource
81
     * @throws RestException
82
     */
83 19
    public function findById($id)
84
    {
85 19
        if (null === ($entity = $this->find($id))) {
86 3
            throw RestException::createNotFound($id, $this->getResourceKey(), sprintf(
87 3
                'Entity of type `%s` not found.', $this->getClassName()
88
            ));
89
        }
90
91 18
        if (!$entity instanceof JsonApiResource) {
92 1
            throw RestException::notJsonApiResource($entity);
93
        }
94
95 17
        return $entity;
96
    }
97
98
    /**
99
     * @param RestRequestContract $request
100
     * @param JsonApiResource     $resource
101
     *
102
     * @return string
103
     */
104 2
    public function linkJsonApiResource(RestRequestContract $request, JsonApiResource $resource)
105
    {
106 2
        return sprintf('%s/%s/%s', $request->getBaseUrl(), $resource->getResourceKey(), $resource->getId());
107
    }
108
}
109