Completed
Push — master ( b7cea0...6248c9 )
by Pavel
02:17
created

RestAction::setResponseFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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