Completed
Push — master ( a5189e...da7714 )
by Pavel
02:31
created

RelatedCollectionAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A repository() 0 3 1
A base() 0 3 1
A __construct() 0 5 1
A applyFilter() 0 11 1
1
<?php namespace Pz\Doctrine\Rest\Action\Related;
2
3
use Pz\Doctrine\Rest\Action\CollectionAction as BaseCollectionAction;
4
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
5
use Pz\Doctrine\Rest\RestRepository;
6
7
use Doctrine\Common\Collections\Criteria;
8
use Doctrine\ORM\QueryBuilder;
9
use League\Fractal\TransformerAbstract;
10
use Pz\Doctrine\Rest\Traits\RelatedAction;
11
12
/**
13
* Action for providing collection (list or array) of data with API.
14
*/
15
class RelatedCollectionAction extends BaseCollectionAction
16
{
17
    use RelatedAction;
18
19
    /**
20
     * RelatedRestAction constructor.
21
     *
22
     * @param RestRepository      $repository
23
     * @param string              $mappedBy
24
     * @param RestRepository      $related
25
     * @param TransformerAbstract $transformer
26
     */
27 5
    public function __construct(RestRepository $repository, $mappedBy, RestRepository $related, $transformer)
28
    {
29 5
        parent::__construct($repository, $transformer);
30 5
        $this->mappedBy = $mappedBy;
31 5
        $this->related = $related;
32 5
    }
33
34
    /**
35
     * Related repository used as default repository for collection queries.
36
     *
37
     * @return RestRepository
38
     */
39 5
    public function repository()
40
    {
41 5
        return $this->related;
42
    }
43
44
    /**
45
     * Base entity repository.
46
     *
47
     * @return RestRepository
48
     */
49 5
    public function base()
50
    {
51 5
        return $this->repository;
52
    }
53
54
    /**
55
     * Add filter by relation entity.
56
     *
57
     * @param RestRequestContract $request
58
     * @param QueryBuilder        $qb
59
     *
60
     * @return $this
61
     * @throws \Pz\Doctrine\Rest\Exceptions\RestException
62
     */
63 5
    protected function applyFilter(RestRequestContract $request, QueryBuilder $qb)
64
    {
65 5
        $entity = $this->base()->findById($request->getId());
66
67 5
        $relateCriteria = Criteria::create();
68 5
        $relateCriteria->andWhere($relateCriteria->expr()->eq($this->mappedBy(), $entity->getId()));
69
70 5
        $qb->innerJoin($qb->getRootAliases()[0].'.'.$this->mappedBy(), $this->mappedBy());
71 5
        $qb->addCriteria($relateCriteria);
72
73 5
        parent::applyFilter($request, $qb);
74 5
    }
75
}
76