Completed
Push — master ( 362ace...e881d8 )
by Pavel
03:36
created

RelatedCollectionAction::base()   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 0
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 Doctrine\ORM\QueryBuilder;
5
6
use League\Fractal\TransformerAbstract;
7
8
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
9
use Pz\Doctrine\Rest\RestRepository;
10
use Pz\LaravelDoctrine\Rest\Tests\App\Entities\User;
0 ignored issues
show
Bug introduced by
The type Pz\LaravelDoctrine\Rest\Tests\App\Entities\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
* Action for providing collection (list or array) of data with API.
14
*/
15
class RelatedCollectionAction extends CollectionAction
16
{
17
    /**
18
     * Repository of basic class.
19
     *
20
     * @var RestRepository
21
     */
22
    protected $base;
23
24
	/**
25
	* Field that contains the related resourse key
26
	*
27
	* @var string
28
	*/
29
	protected $relatedField;
30
31
    /**
32
     * RelatedCollectionAction constructor.
33
     *
34
     * @param RestRepository      $base
35
     * @param string              $relatedField Relation property on related entity
36
     * @param RestRepository      $repository
37
     * @param TransformerAbstract $transformer
38
     */
39 1
	public function __construct(RestRepository $base, $relatedField, RestRepository $repository, $transformer)
40
	{
41 1
		parent::__construct($repository, $transformer);
42 1
		$this->relatedField = $relatedField;
43 1
        $this->base = $base;
44 1
	}
45
46
    /**
47
     * @return RestRepository
48
     */
49 1
    protected function base()
50
    {
51 1
        return $this->base;
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 1
    protected function applyFilter(RestRequestContract $request, QueryBuilder $qb)
64
    {
65
        /** @var User $entity */
66 1
        $entity = $this->base()->findByIdentifier($request);
67
68 1
        $relateCriteria = Criteria::create();
69 1
        $relateCriteria->andWhere($relateCriteria->expr()->eq($this->relatedField, $entity->getId()));
70
71 1
        $qb->innerJoin($qb->getRootAliases()[0].'.'.$this->relatedField, $this->relatedField);
72 1
        $qb->addCriteria($relateCriteria);
73
74 1
        parent::applyFilter($request, $qb);
75 1
    }
76
}
77