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\RestResponseFactory; |
10
|
|
|
use Pz\Doctrine\Rest\Traits\RelatedAction; |
11
|
|
|
|
12
|
|
|
class RelatedCollectionDeleteAction extends RestAction |
13
|
|
|
{ |
14
|
|
|
use RelatedAction; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* RelatedCollectionCreateAction constructor. |
18
|
|
|
* |
19
|
|
|
* @param RestRepository $repository |
20
|
|
|
* @param string $field |
21
|
|
|
* @param RestRepository $related |
22
|
|
|
* @param \Closure|TransformerAbstract $transformer |
23
|
|
|
*/ |
24
|
1 |
|
public function __construct(RestRepository $repository, $field, RestRepository $related, $transformer) |
25
|
|
|
{ |
26
|
1 |
|
parent::__construct($repository, $transformer); |
27
|
1 |
|
$this->related = $related; |
28
|
1 |
|
$this->field = $field; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param RestRequestContract $request |
33
|
|
|
* |
34
|
|
|
* @return RestResponse |
35
|
|
|
* @throws RestException |
36
|
|
|
*/ |
37
|
1 |
|
public function handle($request) |
38
|
|
|
{ |
39
|
1 |
|
$entity = $this->repository()->findById($request->getId()); |
40
|
1 |
|
$this->authorize($request, $entity); |
41
|
|
|
|
42
|
1 |
|
foreach ($request->getData() as $raw) { |
43
|
1 |
|
$item = $this->getRelatedEntity($raw); |
44
|
1 |
|
$this->removeRelationItem($entity, $this->field(), $item); |
45
|
1 |
|
$this->related()->getEntityManager()->remove($item); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
$this->repository()->getEntityManager()->flush(); |
49
|
|
|
|
50
|
1 |
|
return RestResponseFactory::noContent(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|