Completed
Push — master ( a5189e...da7714 )
by Pavel
02:31
created

RestAction::addRelationItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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