Completed
Push — master ( 09e7b0...b7cea0 )
by Pavel
03:54
created

RestAction::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php namespace Pz\Doctrine\Rest;
2
3
use League\Fractal\TransformerAbstract;
4
use Pz\Doctrine\Rest\Contracts\JsonApiResource;
5
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
6
use Pz\Doctrine\Rest\Exceptions\RestException;
7
8
abstract class RestAction
9
{
10
    /**
11
     * @var RestRepository
12
     */
13
    protected $repository;
14
15
    /**
16
     * @var RestResponseFactory
17
     */
18
    protected $response;
19
20
    /**
21
     * @var TransformerAbstract|\Closure
22
     */
23
    protected $transformer;
24
25
    /**
26
     * @param RestRequestContract $request
27
     *
28
     * @throws RestException
29
     * @return RestResponse
30
     */
31
    abstract protected function handle($request);
32
33
    /**
34
     * RestActionAbstract constructor.
35
     *
36
     * @param RestRepository                $repository
37
     * @param TransformerAbstract|\Closure  $transformer
38
     * @param RestResponseFactory           $responseFactory
39
     */
40 28
    public function __construct(RestRepository $repository, $transformer, $responseFactory = null)
41
    {
42 28
        $this->repository = $repository;
43 28
        $this->transformer = $transformer;
44 28
        $this->response = $responseFactory ?: new RestResponseFactory();
45 28
    }
46
47
    /**
48
     * @param RestRequestContract $request
49
     * @return RestResponse
50
     * @throws RestException
51
     */
52 28
    public function dispatch(RestRequestContract $request)
53
    {
54
        try {
55
56 28
            return $this->handle($request);
57
58 10
        } catch (RestException $e) {
59 10
            return RestResponse::exception($e);
60
        }
61
    }
62
63
    /**
64
     * @return RestRepository
65
     */
66 26
    public function repository()
67
    {
68 26
        return $this->repository;
69
    }
70
71
    /**
72
     * @return RestResponseFactory
73
     */
74 19
    public function response()
75
    {
76 19
        return $this->response;
77
    }
78
79
    /**
80
     * @return TransformerAbstract|\Closure
81
     */
82 19
    public function transformer()
83
    {
84 19
        return $this->transformer;
85
    }
86
87
    /**
88
     * Authorize rest request.
89
     * Entity will be object for get,update,delete actions.
90
     * Entity will be string for index,create action.
91
     *
92
     * @param RestRequestContract $request
93
     * @param object|string       $entity
94
     *
95
     * @return mixed
96
     */
97 27
    public function authorize(/** @scrutinizer ignore-unused */$request, /** @scrutinizer ignore-unused */$entity)
98
    {
99 27
        return true;
100
    }
101
102
    /**
103
     * @param JsonApiResource $entity
104
     * @param string          $property
105
     *
106
     * @return mixed
107
     * @throws RestException
108
     */
109 5
    protected function getProperty(JsonApiResource $entity, $property)
110
    {
111 5
        $getter = 'get' . ucfirst($property);
112
113 5
        if (!method_exists($entity, $getter)) {
114 1
            throw RestException::missingGetter($entity, $property, $getter);
115
        }
116
117 4
        return $entity->$getter();
118
    }
119
120
    /**
121
     * @param JsonApiResource $entity
122
     * @param string          $property
123
     * @param mixed           $value
124
     *
125
     * @return mixed
126
     * @throws RestException
127
     */
128 4
    protected function setProperty(JsonApiResource $entity, $property, $value)
129
    {
130 4
        $setter = 'set' . ucfirst($property);
131
132 4
        if (!method_exists($entity, $setter)) {
133 1
            throw RestException::missingSetter($entity, $property, $setter);
134
        }
135
136 3
        return $entity->$setter($value);
137
    }
138
139
    /**
140
     * @param JsonApiResource $entity
141
     * @param string          $field
142
     * @param object          $item
143
     *
144
     * @return mixed
145
     * @throws RestException
146
     */
147 3
    protected function addRelationItem(JsonApiResource $entity, $field, $item)
148
    {
149 3
        $adder = 'add' . ucfirst($field);
150
151 3
        if (!method_exists($entity, $adder)) {
152 1
            throw RestException::missingAdder($entity, $field, $adder);
153
        }
154
155 2
        return $entity->$adder($item);
156
    }
157
158
    /**
159
     * @param JsonApiResource $entity
160
     * @param string          $field
161
     * @param object          $item
162
     *
163
     * @return mixed
164
     * @throws RestException
165
     */
166 3
    protected function removeRelationItem(JsonApiResource $entity, $field, $item)
167
    {
168 3
        $remover = 'remove' . ucfirst($field);
169
170 3
        if (!method_exists($entity, $remover)) {
171 1
            throw RestException::missingRemover($entity, $field, $remover);
172
        }
173
174 2
        return $entity->$remover($item);
175
    }
176
177
    /**
178
     * @param RestResponseFactory $response
179
     * @return $this
180
     */
181
    public function setResponseFactory(RestResponseFactory $response)
182
    {
183
        $this->response = $response;
184
        return $this;
185
    }
186
}
187