Passed
Branch master (f24b8d)
by Pavel
01:56
created

IndexAction::buildResponseData()   A

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\Action;
2
3
use Doctrine\Common\Collections\Criteria;
4
use Pz\Doctrine\Rest\BuilderChain\CriteriaChain;
5
use Pz\Doctrine\Rest\QueryParser\FilterableQueryParser;
6
use Pz\Doctrine\Rest\QueryParser\PropertyQueryParser;
7
use Pz\Doctrine\Rest\Request\IndexRequestInterface;
8
use Pz\Doctrine\Rest\RestRepository;
9
use Pz\Doctrine\Rest\RestResponse;
10
use Pz\Doctrine\Rest\RestResponseFactory;
11
12
trait IndexAction
13
{
14
    /**
15
     * Entity alias.
16
     *
17
     * @var string
18
     */
19
    protected $rootAlias;
20
21
    /**
22
     * Doctrine repository from where get data.
23
     *
24
     * @return RestRepository
25
     */
26
    abstract protected function repository();
27
28
    /**
29
     * @return RestResponseFactory
30
     */
31
    abstract protected function response();
32
33
    /**
34
     * @param IndexRequestInterface $request
35
     *
36
     * @return RestResponse
37
     */
38 4
    public function index(IndexRequestInterface $request)
39
    {
40
        try {
41
42 4
            $request->authorize($this->repository()->getClassName());
43 2
            $chain = CriteriaChain::create($this->criteriaBuilders($request));
44
45 2
            $criteria = new Criteria(null,
46 2
                $request->getOrderBy(),
47 2
                $request->getStart(),
48 2
                $request->getLimit()
49
            );
50
51 2
            $qb = $this->repository()
52 2
                ->createQueryBuilder($this->alias())
53 2
                ->addCriteria($chain->process($criteria));
54
55 2
            return $this->response()->index($request, $qb);
56 2
        } catch (\Exception $e) {
57 2
            return $this->response()->exception($e);
58
        }
59
    }
60
61
    /**
62
     * @return string
63
     */
64 2
    protected function alias()
65
    {
66 2
        if ($this->rootAlias === null) {
67 2
            $reflectionClass = new \ReflectionClass($this->repository()->getClassName());
68 2
            $this->rootAlias = strtolower($reflectionClass->getShortName()[0]);
69
        }
70
71 2
        return $this->rootAlias;
72
    }
73
74
    /**
75
     * @param IndexRequestInterface $request
76
     *
77
     * @return array
78
     */
79 2
    protected function criteriaBuilders(IndexRequestInterface $request)
80
    {
81
        return [
82 2
            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...
83 2
            new FilterableQueryParser($request, $this->getFilterable()),
84
        ];
85
    }
86
87
    /**
88
     * Param that can be filtered if query is string.
89
     *
90
     * @return null|string
91
     */
92 2
    protected function getQueryProperty()
93
    {
94 2
        return null;
95
    }
96
97
    /**
98
     * Get list of filterable entity properties.
99
     *
100
     * @return array
101
     */
102 2
    protected function getFilterable()
103
    {
104 2
        return [];
105
    }
106
}
107