Completed
Push — master ( 88a4a7...4a63e0 )
by Pavel
01:44
created

RestAction::authorize()   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 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 TransformerAbstract|\Closure $transformer
36
     */
37 15
    public function __construct(RestRepository $repository, $transformer)
38
    {
39 15
        $this->repository = $repository;
40 15
        $this->transformer = $transformer;
41 15
        $this->response = new RestResponseFactory();
42 15
    }
43
44
    /**
45
     * @param RestRequest $request
46
     *
47
     * @return RestResponse
48
     */
49 15
    public function dispatch(RestRequest $request)
50
    {
51
        try {
52
53 15
            return $this->handle($request);
54
55 2
        } catch (EntityNotFoundException $e) {
56 1
            return RestResponse::notFound($e->getMessage());
57 1
        } catch (RestException $e) {
58 1
            return RestResponse::exception($e);
59
        }
60
    }
61
62
    /**
63
     * @return RestRepository
64
     */
65 15
    public function repository()
66
    {
67 15
        return $this->repository;
68
    }
69
70
    /**
71
     * @return RestResponseFactory
72
     */
73 13
    public function response()
74
    {
75 13
        return $this->response;
76
    }
77
78
    /**
79
     * @return TransformerAbstract|\Closure
80
     */
81 13
    public function transformer()
82
    {
83 13
        return $this->transformer;
84
    }
85
86
    /**
87
     * Authorize rest request.
88
     * Entity will be object for get,update,delete actions.
89
     * Entity will be string for index,create action.
90
     *
91
     * @param RestRequest   $request
92
     * @param object|string $entity
93
     *
94
     * @return mixed
95
     */
96 14
    public function authorize(/** @scrutinizer ignore-unused */$request, /** @scrutinizer ignore-unused */$entity)
97
    {
98 14
        return true;
99
    }
100
101
    /**
102
     * @param RestRequest     $request
103
     * @param JsonApiResource $resource
104
     *
105
     * @return string|null
106
     */
107 1
    protected function linkJsonApiResource(RestRequest $request, JsonApiResource $resource)
108
    {
109 1
        return sprintf('%s/%s/%s', $request->http()->getBaseUrl(), $resource->getResourceKey(), $resource->getId());
110
    }
111
112
    /**
113
     * @param $entity
114
     *
115
     * @return null|string
116
     */
117 13
    protected function getResourceKey($entity)
118
    {
119 13
        if (is_string($entity) && isset(class_implements($entity)[JsonApiResource::class])) {
120 6
            return call_user_func("$entity::getResourceKey");
121
        }
122
123 7
        if (is_object($entity) && $entity instanceof JsonApiResource) {
124 5
            return $entity->getResourceKey();
125
        }
126
127 2
        return null;
128
    }
129
}
130