Completed
Push — master ( 81fe53...e7743d )
by Pavel
02:34
created

RestController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 94
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrator() 0 3 1
A response() 0 3 1
A __construct() 0 19 4
A updateEntity() 0 5 1
A createEntity() 0 5 1
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
    public function __construct(EntityManager $em, RestResponse $response)
49
    {
50
        $this->em = $em;
51
        $this->response = $response;
52
53
        $this->middleware(function($request, \Closure $next) {
54
            try {
55
                /** @var Response $response */
56
                $response =  $next($request);
57
58
                if (isset($response->exception) && $response->exception instanceof \Exception) {
59
                    return $this->response()->exception($response->exception);
60
                }
61
62
            } catch (\Exception $e) {
63
                return $this->response()->exception($e);
64
            }
65
66
            return $response;
67
        });
68
    }
69
70
    /**
71
     * @param CreateRestRequest $request
72
     *
73
     * @return object
74
     * @throws \Exception
75
     */
76
    public function createEntity($request)
77
    {
78
        return $this->hydrator()->hydrate(
79
            $this->repository()->getClassName(),
80
            $request->validated()
81
        );
82
    }
83
84
    /**
85
     * @param UpdateRestRequest $request
86
     * @param                   $entity
87
     *
88
     * @return object
89
     * @throws \Exception
90
     */
91
    public function updateEntity($request, $entity)
92
    {
93
        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
            $entity,
95
            $request->validated()
96
        );
97
    }
98
99
    /**
100
     * @return RestResponse
101
     */
102
    public function response()
103
    {
104
        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...
Unused Code introduced by
The call to Pz\Doctrine\Rest\Respons...Response::transformer() has too many arguments starting with $this->transformer(). ( Ignorable by Annotation )

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

104
        return $this->response->/** @scrutinizer ignore-call */ transformer($this->transformer());

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...
105
    }
106
107
    /**
108
     * @return ArrayHydrator
109
     */
110
    public function hydrator()
111
    {
112
        return new ArrayHydrator($this->em);
113
    }
114
}
115