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

RelationshipsCollectionDeleteAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 14 2
1
<?php namespace Pz\Doctrine\Rest\Action\Relationships;
2
3
use League\Fractal\TransformerAbstract;
4
use Pz\Doctrine\Rest\Exceptions\RestException;
5
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
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 RelationshipsCollectionDeleteAction extends RestAction
12
{
13
    use RelatedAction;
14
15
    /**
16
     * RelatedRestAction constructor.
17
     *
18
     * @param RestRepository      $repository
19
     * @param string              $field
20
     * @param RestRepository      $related
21
     * @param 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
40 1
        $this->authorize($request, $entity);
41
42 1
        foreach ($request->getData() as $removeItem) {
43 1
            $item = $this->getRelatedEntity($removeItem);
44 1
            $this->removeRelationItem($entity, $this->field(), $item);
45
        }
46
47 1
        $this->repository()->getEntityManager()->flush();
48
49 1
        return $this->response()->noContent();
50
    }
51
}
52