Completed
Push — master ( fafa40...e64df4 )
by Pavel
02:38
created

RelatedItemCreateAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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\Resource\Item;
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\CanValidate;
12
use Pz\Doctrine\Rest\Traits\RelatedAction;
13
14
class RelatedItemCreateAction extends RestAction
15
{
16
    use RelatedAction;
17
    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\...RelatedItemCreateAction: $reflFields, $associationMappings
Loading history...
18
    use CanValidate;
19
20
    /**
21
     * RelatedCollectionCreateAction constructor.
22
     *
23
     * @param RestRepository                               $repository
24
     * @param string                                       $field
25
     * @param RestRepository                               $related
26
     * @param \Closure|TransformerAbstract                 $transformer
27
     */
28 1
    public function __construct(RestRepository $repository, $field, RestRepository $related, $transformer)
29
    {
30 1
        parent::__construct($repository, $transformer);
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
        $item = $this->hydrateEntity($this->related()->getClassName(), $request->getData());
47 1
        $this->validateEntity($item);
48 1
        $this->related()->getEntityManager()->persist($item);
49 1
        $this->setProperty($entity, $this->field(), $item);
50
51 1
        $this->related()->getEntityManager()->flush($entity);
52
53 1
        return $this->response()->resource($request,
54 1
            new Item($item, $this->transformer(), $this->related()->getResourceKey())
0 ignored issues
show
Unused Code introduced by
The call to Pz\Doctrine\Rest\Resource\Item::__construct() has too many arguments starting with $this->related()->getResourceKey(). ( Ignorable by Annotation )

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

54
            /** @scrutinizer ignore-call */ 
55
            new Item($item, $this->transformer(), $this->related()->getResourceKey())

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
55
        );
56
    }
57
}
58