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

RelatedItemAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 13 2
A __construct() 0 5 1
1
<?php namespace Pz\Doctrine\Rest\Action\Related;
2
3
use Pz\Doctrine\Rest\Action\ItemAction as BaseItemAction;
4
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
5
use Pz\Doctrine\Rest\Resource\Item;
6
use Pz\Doctrine\Rest\Resource\NullResource;
7
use Pz\Doctrine\Rest\RestResponse;
8
use Pz\Doctrine\Rest\RestRepository;
9
use Pz\Doctrine\Rest\Traits\RelatedAction;
10
11
use League\Fractal\TransformerAbstract;
12
13
class RelatedItemAction extends BaseItemAction
14
{
15
    use RelatedAction;
16
17
    /**
18
     * RelatedRestAction constructor.
19
     *
20
     * @param RestRepository      $repository
21
     * @param string              $field
22
     * @param RestRepository      $related
23
     * @param TransformerAbstract $transformer
24
     */
25 2
    public function __construct(RestRepository $repository, $field, RestRepository $related, $transformer)
26
    {
27 2
        parent::__construct($repository, $transformer);
28 2
        $this->related = $related;
29 2
        $this->field = $field;
30 2
    }
31
32
    /**
33
	* @param RestRequestContract $request
34
	*
35
	* @return RestResponse
36
	*/
37 2
	public function handle($request)
38
	{
39 2
		$entity = $this->repository()->findById($request->getId());
40
41 2
        $this->authorize($request, $entity);
42
43 2
        if ($relatedEntity = $this->getProperty($entity, $this->field())) {
44 2
            return $this->response()->resource($request,
45 2
                new Item($relatedEntity, $this->transformer())
46
            );
47
        }
48
49 1
        return $this->response()->resource($request, new NullResource());
50
	}
51
}
52