Completed
Push — master ( 78334d...666726 )
by Pavel
01:41
created

CollectionAction::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 1
rs 9.4285
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\RestActionAbstract;
8
use Pz\Doctrine\Rest\RestRequest;
9
use Pz\Doctrine\Rest\RestResponse;
10
11
/**
12
 * Action for providing collection (list or array) of data with API.
13
 */
14
class CollectionAction extends RestActionAbstract
15
{
16
    /**
17
     * Field that can be filtered if filter is string.
18
     *
19
     * @var string
20
     */
21
    protected $filterProperty;
22
23
    /**
24
     * Get list of filterable entity fields.
25
     *
26
     * @var array
27
     */
28
    protected $filterable = [];
29
30
    /**
31
     * @param string $property
32
     *
33
     * @return $this
34
     */
35 1
    public function setFilterProperty($property)
36
    {
37 1
        $this->filterProperty = $property;
38 1
        return $this;
39
    }
40
41
    /**
42
     * @param array $filterable
43
     *
44
     * @return $this
45
     */
46 1
    public function setFilterable(array $filterable)
47
    {
48 1
        $this->filterable = $filterable;
49 1
        return $this;
50
    }
51
52
    /**
53
     * @param RestRequest $request
54
     *
55
     * @return RestResponse
56
     */
57 6
    protected function handle(RestRequest $request)
58
    {
59 6
        $request->authorize($this->repository()->getClassName());
60 6
        $chain = CriteriaChain::create($this->criteriaBuilders($request));
61
62 6
        $criteria = new Criteria(null,
63 6
            $request->getOrderBy(),
64 6
            $request->getStart(),
65 6
            $request->getLimit()
66
        );
67
68 6
        $qb = $this->repository()
69 6
            ->createQueryBuilder($this->repository()->alias())
70 6
            ->addCriteria($chain->process($criteria));
71
72 6
        return $this->response()->collection($request, $qb);
73
    }
74
75
    /**
76
     * @param RestRequest $request
77
     *
78
     * @return array
79
     */
80 6
    protected function criteriaBuilders(RestRequest $request)
81
    {
82
        return [
83 6
            new PropertyQueryParser($request, $this->getQueryProperty()),
84 6
            new FilterableQueryParser($request, $this->getFilterable()),
85
        ];
86
    }
87
88
    /**
89
     * Param that can be filtered if query is string.
90
     *
91
     * @return null|string
92
     */
93 6
    protected function getQueryProperty()
94
    {
95 6
        return $this->filterProperty;
96
    }
97
98
    /**
99
     * Get list of filterable entity properties.
100
     *
101
     * @return array
102
     */
103 6
    protected function getFilterable()
104
    {
105 6
        return $this->filterable;
106
    }
107
}
108