RelatedCollectionCreateAction::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
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\CanHydrate;
10
use Pz\Doctrine\Rest\Traits\CanValidate;
11
use Pz\Doctrine\Rest\Traits\RelatedAction;
12
13
class RelatedCollectionCreateAction 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\...dCollectionCreateAction: $reflFields, $associationMappings
Loading history...
17
    use CanValidate;
18
19
    /**
20
     * RelatedCollectionCreateAction constructor.
21
     *
22
     * @param RestRepository                               $repository
23
     * @param string                                       $field
24
     * @param                                              $mappedBy
25
     * @param RestRepository                               $related
26
     * @param \Closure|TransformerAbstract                 $transformer
27
     */
28 1
    public function __construct(RestRepository $repository, $field, $mappedBy, RestRepository $related, $transformer)
29
    {
30 1
        parent::__construct($repository, $transformer);
31 1
        $this->mappedBy = $mappedBy;
32 1
        $this->related = $related;
33 1
        $this->field = $field;
34 1
    }
35
36
    /**
37
     * @param RestRequestContract $request
38
     *
39
     * @return RestResponse
40
     * @throws RestException
41
     */
42 1
    public function handle($request)
43
    {
44 1
        $entity = $this->repository()->findById($request->getId());
45 1
        $this->authorize($request, $entity);
46
47 1
        foreach ($request->getData() as $raw) {
48 1
            $item = $this->hydrateEntity($this->related()->getClassName(), $raw);
49 1
            $this->addRelationItem($entity, $this->field(), $item);
50 1
            $this->validateEntity($item);
51 1
            $this->related()->getEntityManager()->persist($item);
52
        }
53
54 1
        $this->repository()->getEntityManager()->flush($entity);
55
56 1
        return (new RelatedCollectionAction(
57 1
            $this->repository(),
58 1
            $this->mappedBy(),
59 1
            $this->related(),
60 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

60
            /** @scrutinizer ignore-type */ $this->transformer()
Loading history...
61 1
        ))->dispatch($request)->setStatusCode(RestResponse::HTTP_CREATED);
62
    }
63
}
64