|
1
|
|
|
<?php namespace Pz\Doctrine\Rest\Action\Relationships; |
|
2
|
|
|
|
|
3
|
|
|
use League\Fractal\TransformerAbstract; |
|
4
|
|
|
use Pz\Doctrine\Rest\Action\Related\RelatedCollectionAction; |
|
5
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
|
6
|
|
|
use Pz\Doctrine\Rest\Exceptions\RestException; |
|
7
|
|
|
use Pz\Doctrine\Rest\RestAction; |
|
8
|
|
|
use Pz\Doctrine\Rest\RestRepository; |
|
9
|
|
|
use Pz\Doctrine\Rest\RestResponse; |
|
10
|
|
|
use Pz\Doctrine\Rest\Traits\CanHydrate; |
|
11
|
|
|
use Pz\Doctrine\Rest\Traits\RelatedAction; |
|
12
|
|
|
|
|
13
|
|
|
class RelationshipsCollectionCreateAction extends RestAction |
|
14
|
|
|
{ |
|
15
|
|
|
use RelatedAction; |
|
16
|
|
|
use CanHydrate; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* RelatedRestAction constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param RestRepository $repository |
|
22
|
|
|
* @param string $field |
|
23
|
|
|
* @param string $mappedBy |
|
24
|
|
|
* @param RestRepository $related |
|
25
|
|
|
* @param TransformerAbstract $transformer |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public function __construct(RestRepository $repository, $field, $mappedBy, RestRepository $related, $transformer) |
|
28
|
|
|
{ |
|
29
|
2 |
|
parent::__construct($repository, $transformer); |
|
30
|
2 |
|
$this->mappedBy = $mappedBy; |
|
31
|
2 |
|
$this->related = $related; |
|
32
|
2 |
|
$this->field = $field; |
|
33
|
2 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param RestRequestContract $request |
|
37
|
|
|
* |
|
38
|
|
|
* @return RestResponse |
|
39
|
|
|
* @throws RestException |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function handle($request) |
|
42
|
|
|
{ |
|
43
|
2 |
|
$entity = $this->repository()->findById($request->getId()); |
|
44
|
2 |
|
$this->authorize($request, $entity); |
|
45
|
|
|
|
|
46
|
2 |
|
foreach ($request->getData() as $raw) { |
|
47
|
2 |
|
$item = $this->getRelatedEntity($raw); |
|
48
|
2 |
|
$this->addRelationItem($entity, $this->field(), $item); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
$this->repository()->getEntityManager()->flush($entity); |
|
52
|
|
|
|
|
53
|
|
|
return ( |
|
54
|
1 |
|
new RelatedCollectionAction( |
|
55
|
1 |
|
$this->repository(), |
|
56
|
1 |
|
$this->mappedBy(), |
|
57
|
1 |
|
$this->related(), |
|
58
|
1 |
|
$this->transformer() |
|
|
|
|
|
|
59
|
|
|
) |
|
60
|
1 |
|
)->dispatch($request)->setStatusCode(RestResponse::HTTP_CREATED); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|