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

RelationshipsCollectionUpdateAction::__construct()   A

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 0
Metric Value
cc 1
eloc 4
nc 1
nop 5
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
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\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\RelatedAction;
11
12
class RelationshipsCollectionUpdateAction extends RestAction
13
{
14
    use RelatedAction;
15
    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\...sCollectionUpdateAction: $reflFields, $associationMappings
Loading history...
16
17
    /**
18
     * RelatedRestAction constructor.
19
     *
20
     * @param RestRepository      $repository
21
     * @param string              $field
22
     * @param string              $mappedBy
23
     * @param RestRepository      $related
24
     * @param TransformerAbstract $transformer
25
     */
26 1
    public function __construct(RestRepository $repository, $field, $mappedBy, RestRepository $related, $transformer)
27
    {
28 1
        parent::__construct($repository, $transformer);
29 1
        $this->mappedBy = $mappedBy;
30 1
        $this->related = $related;
31 1
        $this->field = $field;
32 1
    }
33
34
    /**
35
     * @param RestRequestContract $request
36
     *
37
     * @return RestResponse
38
     */
39 1
    public function handle($request)
40
    {
41 1
        $entity = $this->repository()->findById($request->getId());
42
43 1
        $this->authorize($request, $entity);
44
45 1
        $items = array_map(
46 1
            function($raw) use ($entity) {
0 ignored issues
show
Unused Code introduced by
The import $entity is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
47 1
                return $this->getRelatedEntity($raw);
48 1
            },
49
            $request->getData()
50
        );
51
52
        $this->setProperty($entity, $this->field(), $items);
53
54
        $this->repository()->getEntityManager()->flush($entity);
55
56
        return (
57
            new RelatedCollectionAction(
58
                $this->repository(),
59
                $this->mappedBy(),
60
                $this->related(),
61
                $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

61
                /** @scrutinizer ignore-type */ $this->transformer()
Loading history...
62
            )
63
        )->dispatch($request);
64
    }
65
}
66