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

RelatedCollectionDeleteAction::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 2
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 RelatedCollectionDeleteAction 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
        foreach ($request->getData() as $raw) {
42 1
            $item = $this->getRelatedEntity($raw);
43 1
            $this->removeRelationItem($entity, $this->field(), $item);
44 1
            $this->related()->getEntityManager()->remove($item);
45
        }
46
47 1
        $this->repository()->getEntityManager()->flush();
48
49 1
        return $this->response()->noContent();
50
    }
51
}
52