Test Failed
Push — master ( 1f2e19...0342a7 )
by Pavel
01:42
created

RestAction   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
dl 0
loc 111
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A response() 0 3 1
A repository() 0 3 1
A linkJsonApiResource() 0 3 1
B getResourceKey() 0 11 5
A transformer() 0 3 1
A __construct() 0 9 1
A dispatch() 0 10 3
1
<?php namespace Pz\Doctrine\Rest;
2
3
use Doctrine\ORM\EntityNotFoundException;
4
use League\Fractal\TransformerAbstract;
5
use Pz\Doctrine\Rest\Contracts\JsonApiResource;
6
7
abstract class RestAction
8
{
9
    /**
10
     * @var RestRepository
11
     */
12
    protected $repository;
13
14
    /**
15
     * @var RestResponseFactory
16
     */
17
    protected $response;
18
19
    /**
20
     * @var TransformerAbstract|\Closure
21
     */
22
    protected $transformer;
23
24
    /**
25
     * @param RestRequest $request
26
     *
27
     * @return RestResponse
28
     */
29
    abstract protected function handle(RestRequest $request);
30
31
    /**
32
     * RestActionAbstract constructor.
33
     *
34
     * @param RestRepository               $repository
35
     * @param RestResponseFactory          $response
36
     * @param TransformerAbstract|\Closure $transformer
37
     */
38 14
    public function __construct(
39
        RestRepository $repository,
40
        RestResponseFactory $response,
41
        $transformer
42
    )
43
    {
44 14
        $this->repository = $repository;
45 14
        $this->transformer = $transformer;
46 14
        $this->response = $response;
47 14
    }
48
49
    /**
50
     * @param RestRequest $request
51
     *
52
     * @return RestResponse
53
     */
54 14
    public function dispatch(RestRequest $request)
55
    {
56
        try {
57
58 14
            return $this->handle($request);
59
60 1
        } catch (EntityNotFoundException $e) {
61
            return RestResponse::notFound($e->getMessage());
62 1
        } catch (RestException $e) {
63 1
            return RestResponse::exception($e);
64
        }
65
    }
66
67
    /**
68
     * @return RestRepository
69
     */
70 14
    public function repository()
71
    {
72 14
        return $this->repository;
73
    }
74
75
    /**
76
     * @return RestResponseFactory
77
     */
78 13
    public function response()
79
    {
80 13
        return $this->response;
81
    }
82
83
    /**
84
     * @return TransformerAbstract
85
     */
86 13
    public function transformer()
87
    {
88 13
        return $this->transformer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->transformer also could return the type Closure which is incompatible with the documented return type League\Fractal\TransformerAbstract.
Loading history...
89
    }
90
91
    /**
92
     * @param RestRequest     $request
93
     * @param JsonApiResource $resource
94
     *
95
     * @return string|null
96
     */
97 1
    protected function linkJsonApiResource(RestRequest $request, JsonApiResource $resource)
98
    {
99 1
        return sprintf('%s/%s/%s', $request->http()->getBaseUrl(), $resource->getResourceKey(), $resource->getId());
100
    }
101
102
    /**
103
     * @param $entity
104
     *
105
     * @return null|string
106
     */
107 13
    protected function getResourceKey($entity)
108
    {
109 13
        if (is_string($entity) && isset(class_implements($entity)[JsonApiResource::class])) {
110 6
            return call_user_func("$entity::getResourceKey");
111
        }
112
113 7
        if (is_object($entity) && $entity instanceof JsonApiResource) {
114 5
            return $entity->getResourceKey();
115
        }
116
117 2
        return null;
118
    }
119
}
120