Test Failed
Push — master ( e7743d...99f6c2 )
by Pavel
02:49
created

RestController::response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Pz\LaravelDoctrine\Rest;
2
3
use Doctrine\ORM\EntityManager;
4
use Illuminate\Http\Response;
5
use Illuminate\Routing\Controller;
6
use League\Fractal\TransformerAbstract;
7
8
use pmill\Doctrine\Hydrator\ArrayHydrator;
9
use Pz\Doctrine\Rest\Action\DeleteAction;
10
use Pz\Doctrine\Rest\Action\IndexAction;
11
use Pz\Doctrine\Rest\Action\ShowAction;
12
use Pz\Doctrine\Rest\Action\CreateAction;
13
use Pz\Doctrine\Rest\Action\UpdateAction;
14
15
use Pz\Doctrine\Rest\RestResponseFactory;
16
use Pz\LaravelDoctrine\Rest\Request\CreateRestRequest;
17
use Pz\LaravelDoctrine\Rest\Request\UpdateRestRequest;
18
19
abstract class RestController extends Controller
20
{
21
    use IndexAction;
22
    use ShowAction;
23
    use DeleteAction;
24
    use CreateAction;
25
    use UpdateAction;
26
27
    /**
28
     * @var EntityManager
29
     */
30
    protected $em;
31
32
    /**
33
     * @var RestResponse|RestResponseFactory
34
     */
35
    protected $response;
36
37
    /**
38
     * @return TransformerAbstract
39
     */
40
    abstract public function transformer();
41
42
    /**
43
     * UserController constructor.
44
     *
45
     * @param EntityManager $em
46
     * @param RestResponse  $response
47
     */
48 5
    public function __construct(EntityManager $em, RestResponse $response)
49
    {
50 5
        $this->em = $em;
51 5
        $this->response = $response;
52
53 5
        $this->middleware(function($request, \Closure $next) {
54
            try {
55
                /** @var Response $response */
56 5
                $response =  $next($request);
57
58 5
                if (isset($response->exception) && $response->exception instanceof \Exception) {
59 5
                    return $this->response()->exception($response->exception);
60
                }
61
62 5
            } catch (\Exception $e) {
63 5
                return $this->response()->exception($e);
64
            }
65
66 5
            return $response;
67 5
        });
68 5
    }
69
70
    /**
71
     * @param CreateRestRequest $request
72
     *
73
     * @return object
74
     * @throws \Exception
75
     */
76 1
    public function createEntity($request)
77
    {
78 1
        return $this->hydrator()->hydrate(
79 1
            $this->repository()->getClassName(),
80 1
            $request->validated()
81
        );
82
    }
83
84
    /**
85
     * @param UpdateRestRequest $request
86
     * @param                   $entity
87
     *
88
     * @return object
89
     * @throws \Exception
90
     */
91 1
    public function updateEntity($request, $entity)
92
    {
93 1
        return $this->hydrator()->hydrate(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->hydrator()->hydra... $request->validated()) 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...
94 1
            $entity,
95 1
            $request->validated()
96
        );
97
    }
98
99
    /**
100
     * @return RestResponse
101
     */
102 5
    public function response()
103
    {
104 5
        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...
105
    }
106
107
    /**
108
     * @return ArrayHydrator
109
     */
110 2
    public function hydrator()
111
    {
112 2
        return new ArrayHydrator($this->em);
113
    }
114
}
115