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

RelationshipsCollectionCreateAction::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 1
dl 0
loc 20
ccs 13
cts 13
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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;
0 ignored issues
show
introduced by
The trait Pz\Doctrine\Rest\Traits\CanHydrate requires some properties which are not provided by Pz\Doctrine\Rest\Action\...sCollectionCreateAction: $reflFields, $associationMappings
Loading history...
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 1
    public function __construct(RestRepository $repository, $field, $mappedBy, RestRepository $related, $transformer)
28
    {
29 1
        parent::__construct($repository, $transformer);
30 1
        $this->mappedBy = $mappedBy;
31 1
        $this->related = $related;
32 1
        $this->field = $field;
33 1
    }
34
35
    /**
36
     * @param RestRequestContract $request
37
     *
38
     * @return RestResponse
39
     * @throws RestException
40
     */
41 1
    public function handle($request)
42
    {
43 1
        $entity = $this->repository()->findById($request->getId());
44 1
        $this->authorize($request, $entity);
45
46 1
        foreach ($request->getData() as $raw) {
47 1
            $item = $this->getRelatedEntity($raw);
48 1
            $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()
0 ignored issues
show
Bug introduced by
It seems like $this->transformer() can also be of type Closure; however, parameter $transformer of Pz\Doctrine\Rest\Action\...onAction::__construct() does only seem to accept League\Fractal\TransformerAbstract, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
                /** @scrutinizer ignore-type */ $this->transformer()
Loading history...
59
            )
60 1
        )->dispatch($request);
61
    }
62
}
63