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
|
|
|
|