Completed
Push — master ( fafa40...e64df4 )
by Pavel
02:38
created

RelatedItemDeleteAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest\Action\Related;
2
3
use League\Fractal\TransformerAbstract;
4
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
5
use Pz\Doctrine\Rest\Exceptions\RestException;
6
use Pz\Doctrine\Rest\RestAction;
7
use Pz\Doctrine\Rest\RestRepository;
8
use Pz\Doctrine\Rest\RestResponse;
9
use Pz\Doctrine\Rest\Traits\RelatedAction;
10
11
class RelatedItemDeleteAction extends RestAction
12
{
13
    use RelatedAction;
14
15
    /**
16
     * RelatedCollectionCreateAction constructor.
17
     *
18
     * @param RestRepository                               $repository
19
     * @param string                                       $field
20
     * @param RestRepository                               $related
21
     * @param \Closure|TransformerAbstract                 $transformer
22
     */
23 1
    public function __construct(RestRepository $repository, $field, RestRepository $related, $transformer)
24
    {
25 1
        parent::__construct($repository, $transformer);
26 1
        $this->related = $related;
27 1
        $this->field = $field;
28 1
    }
29
30
    /**
31
     * @param RestRequestContract $request
32
     *
33
     * @return RestResponse
34
     * @throws RestException
35
     */
36 1
    public function handle($request)
37
    {
38 1
        $entity = $this->repository()->findById($request->getId());
39 1
        $this->authorize($request, $entity);
40
41 1
        $item = $this->getProperty($entity, $this->field());
42 1
        $this->related()->getEntityManager()->remove($item);
43 1
        $this->setProperty($entity, $this->field(), null);
44
45 1
        $this->repository()->getEntityManager()->flush();
46
47 1
        return $this->response()->noContent();
48
    }
49
}
50