Test Failed
Push — master ( b600aa...6380c9 )
by Pavel
02:04
created

IndexAction::index()   B

Complexity

Conditions 3
Paths 11

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.5293

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 11
nop 1
dl 0
loc 26
ccs 11
cts 18
cp 0.6111
crap 3.5293
rs 8.8571
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest\Action;
2
3
use Doctrine\Common\Collections\Criteria;
4
use Doctrine\ORM\Query;
5
use Doctrine\ORM\QueryBuilder;
6
use Pz\Doctrine\Rest\Action\Index\ResponseData;
7
use Pz\Doctrine\Rest\BuilderChain\CriteriaChain;
8
use Pz\Doctrine\Rest\QueryParser\FilterableQueryParser;
9
use Pz\Doctrine\Rest\QueryParser\PropertyQueryParser;
10
use Pz\Doctrine\Rest\Request\IndexRequestInterface;
11
use Pz\Doctrine\Rest\RestException;
12
use Pz\Doctrine\Rest\RestRepository;
13
use Pz\Doctrine\Rest\RestResponse;
14
use Pz\Doctrine\Rest\RestResponseFactory;
15
16
trait IndexAction
17
{
18
    /**
19
     * Entity alias.
20
     *
21
     * @var string
22
     */
23
    protected $rootAlias;
24
25
    /**
26
     * Doctrine repository from where get data.
27
     *
28
     * @return RestRepository
29
     */
30
    abstract protected function repository();
31
32
    /**
33
     * @return RestResponseFactory
34
     */
35
    abstract protected function response();
36
37
    /**
38
     * @param IndexRequestInterface $request
39
     *
40
     * @return array
41
     */
42 1
    public function index(IndexRequestInterface $request)
43
    {
44
        try {
45
46 1
            $request->authorize($this->repository()->getClassName());
47 1
            $chain = CriteriaChain::create($this->criteriaBuilders($request));
48
49 1
            $criteria = new Criteria(null,
50 1
                $request->getOrderBy(),
51 1
                $request->getStart(),
52 1
                $request->getLimit()
53
            );
54
55 1
            $qb = $this->repository()
56 1
                ->createQueryBuilder($this->alias())
57 1
                ->addCriteria($chain->process($criteria));
58
59 1
            return $this->response()->index($request, $this->buildResponseData($qb));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response()...buildResponseData($qb)) returns the type Pz\Doctrine\Rest\RestResponse which is incompatible with the documented return type array.
Loading history...
60
61
        } catch (RestException $e) {
62
            return $this->response()->error($e->getCode(), $e->getMessage(), $e->errors());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response()...essage(), $e->errors()) returns the type Pz\Doctrine\Rest\RestResponse which is incompatible with the documented return type array.
Loading history...
63
        } catch (\Exception $e) {
64
            return $this->response()->error(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response()...sage(), $e->getTrace()) returns the type Pz\Doctrine\Rest\RestResponse which is incompatible with the documented return type array.
Loading history...
65
                RestResponse::HTTP_INTERNAL_SERVER_ERROR,
66
                $e->getMessage(),
67
                $e->getTrace()
68
            );
69
        }
70
    }
71
72
    /**
73
     * @param QueryBuilder $qb
74
     *
75
     * @return ResponseData
76
     */
77 1
    protected function buildResponseData(QueryBuilder $qb)
78
    {
79 1
        return new ResponseData($qb);
80
    }
81
82
    /**
83
     * @return string
84
     */
85 1
    protected function alias()
86
    {
87 1
        if ($this->rootAlias === null) {
88 1
            $reflectionClass = new \ReflectionClass($this->repository()->getClassName());
89 1
            $this->rootAlias = strtolower($reflectionClass->getShortName()[0]);
90
        }
91
92 1
        return $this->rootAlias;
93
    }
94
95
    /**
96
     * @param IndexRequestInterface $request
97
     *
98
     * @return array
99
     */
100 1
    protected function criteriaBuilders(IndexRequestInterface $request)
101
    {
102
        return [
103 1
            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...
104 1
            new FilterableQueryParser($request, $this->getFilterable()),
105
        ];
106
    }
107
108
    /**
109
     * Param that can be filtered if query is string.
110
     *
111
     * @return null|string
112
     */
113 1
    protected function getQueryProperty()
114
    {
115 1
        return null;
116
    }
117
118
    /**
119
     * Get list of filterable entity properties.
120
     *
121
     * @return array
122
     */
123 1
    protected function getFilterable()
124
    {
125 1
        return [];
126
    }
127
}
128