Completed
Push — master ( c0b3e5...c9ae9e )
by Pavel
03:09
created

IndexAction::getQueryProperty()   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\Action;
2
3
use Doctrine\Common\Collections\Criteria;
4
use Pz\Doctrine\Rest\BuilderChain\CriteriaChain;
5
use Pz\Doctrine\Rest\Contracts\HasResourceKey;
6
use Pz\Doctrine\Rest\QueryParser\FilterableQueryParser;
7
use Pz\Doctrine\Rest\QueryParser\PropertyQueryParser;
8
use Pz\Doctrine\Rest\Request\IndexRequestInterface;
9
use Pz\Doctrine\Rest\RestRepository;
10
use Pz\Doctrine\Rest\RestResponse;
11
use Pz\Doctrine\Rest\RestResponseFactory;
12
13
trait IndexAction
14
{
15
    /**
16
     * Entity alias.
17
     *
18
     * @var string
19
     */
20
    protected $rootAlias;
21
22
    /**
23
     * Doctrine repository from where get data.
24
     *
25
     * @return RestRepository
26
     */
27
    abstract protected function repository();
28
29
    /**
30
     * @return RestResponseFactory
31
     */
32
    abstract protected function response();
33
34
    /**
35
     * @param IndexRequestInterface $request
36
     *
37
     * @return RestResponse
38
     */
39 2
    public function index(IndexRequestInterface $request)
40
    {
41
        try {
42
43 2
            $request->authorize($this->repository()->getClassName());
44
            $chain = CriteriaChain::create($this->criteriaBuilders($request));
45
46
            $criteria = new Criteria(null,
47
                $request->getOrderBy(),
48
                $request->getStart(),
49
                $request->getLimit()
50
            );
51
52
            $qb = $this->repository()
53
                ->createQueryBuilder($this->alias())
54
                ->addCriteria($chain->process($criteria));
55
56
            return $this->response()->index($request, $qb);
57 2
        } catch (\Exception $e) {
58 2
            return $this->response()->exception($e);
59
        }
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    protected function alias()
66
    {
67
        if ($this->rootAlias === null) {
68
69
            $reflectionClass = $this->repository()->em()
70
                ->getClassMetadata($this->repository()->getClassName())
71
                ->getReflectionClass();
72
73
            if ($reflectionClass->implementsInterface(HasResourceKey::class)) {
74
                $this->rootAlias = call_user_func($reflectionClass->getName(). '::getResourceKey');
75
            } else {
76
                // Camel case to underscore-case
77
                $this->rootAlias = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $reflectionClass->getShortName()));
78
            }
79
        }
80
81
        return $this->rootAlias;
82
    }
83
84
    /**
85
     * @param IndexRequestInterface $request
86
     *
87
     * @return array
88
     */
89
    protected function criteriaBuilders(IndexRequestInterface $request)
90
    {
91
        return [
92
            new PropertyQueryParser($request, $this->getQueryProperty()),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getQueryProperty() targeting Pz\Doctrine\Rest\Action\...ion::getQueryProperty() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
93
            new FilterableQueryParser($request, $this->getFilterable()),
94
        ];
95
    }
96
97
    /**
98
     * Param that can be filtered if query is string.
99
     *
100
     * @return null|string
101
     */
102
    protected function getQueryProperty()
103
    {
104
        return null;
105
    }
106
107
    /**
108
     * Get list of filterable entity properties.
109
     *
110
     * @return array
111
     */
112
    protected function getFilterable()
113
    {
114
        return [];
115
    }
116
}
117