Completed
Push — master ( 99f6c2...5bcbf0 )
by Pavel
05:52
created

RestController::hydrator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Pz\LaravelDoctrine\Rest;
2
3
use App\JsonApiHydrator;
0 ignored issues
show
Bug introduced by
The type App\JsonApiHydrator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
4
use Doctrine\ORM\EntityManager;
5
use Illuminate\Http\Response;
6
use Illuminate\Routing\Controller;
7
use League\Fractal\TransformerAbstract;
8
9
use pmill\Doctrine\Hydrator\ArrayHydrator;
10
use Pz\Doctrine\Rest\Action\DeleteAction;
11
use Pz\Doctrine\Rest\Action\IndexAction;
12
use Pz\Doctrine\Rest\Action\ShowAction;
13
use Pz\Doctrine\Rest\Action\CreateAction;
14
use Pz\Doctrine\Rest\Action\UpdateAction;
15
16
use Pz\Doctrine\Rest\RestResponseFactory;
17
use Pz\LaravelDoctrine\Rest\Request\CreateRestRequest;
18
use Pz\LaravelDoctrine\Rest\Request\UpdateRestRequest;
19
20
abstract class RestController extends Controller
21
{
22
    use IndexAction;
23
    use ShowAction;
24
    use DeleteAction;
25
    use CreateAction;
26
    use UpdateAction;
27
28
    /**
29
     * @var EntityManager
30
     */
31
    protected $em;
32
33
    /**
34
     * @var RestResponse|RestResponseFactory
35
     */
36
    protected $response;
37
38
    /**
39
     * @return TransformerAbstract
40
     */
41
    abstract public function transformer();
42
43
    /**
44
     * UserController constructor.
45
     *
46
     * @param EntityManager $em
47
     * @param RestResponse  $response
48
     */
49 4
    public function __construct(EntityManager $em, RestResponse $response)
50
    {
51 4
        $this->em = $em;
52 4
        $this->response = $response;
53
54 4
        $this->middleware(function($request, \Closure $next) {
55
            try {
56
                /** @var Response $response */
57 4
                $response =  $next($request);
58
59 4
                if (isset($response->exception) && $response->exception instanceof \Exception) {
60 4
                    return $this->response()->exception($response->exception);
61
                }
62
63 4
            } catch (\Exception $e) {
64 4
                return $this->response()->exception($e);
65
            }
66
67
            return $response;
68 4
        });
69 4
    }
70
71
    /**
72
     * @param CreateRestRequest $request
73
     *
74
     * @return object
75
     * @throws \Exception
76
     */
77
    public function createEntity($request)
78
    {
79
        return $this->hydrator($request)
80
            ->hydrate($this->repository()->getClassName(), $request->all());
81
    }
82
83
    /**
84
     * @param UpdateRestRequest $request
85
     * @param                   $entity
86
     *
87
     * @return object
88
     * @throws \Exception
89
     */
90
    public function updateEntity($request, $entity)
91
    {
92
        return $this->hydrator($request)
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->hydrator($request...ntity, $request->all()) targeting pmill\Doctrine\Hydrator\ArrayHydrator::hydrate() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
93
            ->hydrate($entity, $request->all());
94
    }
95
96
    /**
97
     * @return RestResponse
98
     */
99 4
    public function response()
100
    {
101 4
        return $this->response->transformer($this->transformer());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response->...r($this->transformer()) returns the type League\Fractal\TransformerAbstract which is incompatible with the documented return type Pz\LaravelDoctrine\Rest\RestResponse.
Loading history...
102
    }
103
104
    /**
105
     * @return ArrayHydrator
106
     */
107
    public function hydrator(RestRequest $request)
108
    {
109
        if ($request->isJsonApi()) {
110
            return new JsonApiHydrator($this->em);
111
        }
112
113
        return new ArrayHydrator($this->em);
114
    }
115
}
116