Completed
Pull Request — master (#413)
by Anton
05:33
created

AbstractMapper::getPrimaryKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 1
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Controller\Mapper;
12
13
use Bluz\Application\Application;
14
use Bluz\Application\Exception\ForbiddenException;
15
use Bluz\Application\Exception\NotImplementedException;
16
use Bluz\Controller\ControllerException;
17
use Bluz\Crud\AbstractCrud;
18
use Bluz\Http\RequestMethod;
19
use Bluz\Proxy\Acl;
20
use Bluz\Proxy\Request;
21
use Bluz\Proxy\Router;
22
23
/**
24
 * Mapper for controller
25
 *
26
 * @package  Bluz\Rest
27
 * @author   Anton Shevchuk
28
 */
29
abstract class AbstractMapper
30
{
31
    /**
32
     * @var string HTTP Method
33
     */
34
    protected $method = RequestMethod::GET;
35
36
    /**
37
     * @var string
38
     */
39
    protected $module;
40
41
    /**
42
     * @var string
43
     */
44
    protected $controller;
45
    
46
    /**
47
     * @var array identifier
48
     */
49
    protected $primary;
50
51
    /**
52
     * @var string relation list
53
     */
54
    protected $relation;
55
56
    /**
57
     * @var string relation Id
58
     */
59
    protected $relationId;
60
61
    /**
62
     * @var array params of query
63
     */
64
    protected $params = [];
65
66
    /**
67
     * @var array query data
68
     */
69
    protected $data = [];
70
71
    /**
72
     * @var AbstractCrud instance of CRUD
73
     */
74
    protected $crud;
75
76
    /**
77
     * [
78
     *     METHOD => [
79
     *         'module' => 'module',
80
     *         'controller' => 'controller',
81
     *         'acl' => 'privilege',
82
     *     ],
83
     * ]
84
     *
85
     * @var array
86
     */
87
    protected $map = [];
88
89
    /**
90
     * Prepare request for processing
91
     */
92 11
    public function __construct()
93
    {
94
        // HTTP method
95 11
        $method = Request::getMethod();
96 11
        $this->method = strtoupper($method);
97
98
        // get path
99
        // %module% / %controller% / %id% / %relation% / %id%
100 11
        $path = Router::getCleanUri();
101
102 11
        $this->params = explode('/', rtrim($path, '/'));
103
104
        // module
105 11
        $this->module = array_shift($this->params);
106
107
        // controller
108 11
        $this->controller = array_shift($this->params);
109
110 11
        $data = Request::getParams();
111
112 11
        unset($data['_method'], $data['_module'], $data['_controller']);
113
114 11
        $this->data = $data;
115 11
    }
116
117
    /**
118
     * Add mapping data
119
     *
120
     * @param string $method
121
     * @param string $module
122
     * @param string $controller
123
     * @param String $acl
124
     */
125 6
    public function addMap($method, $module, $controller, $acl = null)
126
    {
127 6
        $this->map[strtoupper($method)] = [
128 6
            'module' => $module,
129 6
            'controller' => $controller,
130 6
            'acl' => $acl
131
        ];
132 6
    }
133
134
    /**
135
     * Add mapping for HEAD method
136
     *
137
     * @param string $module
138
     * @param string $controller
139
     * @param String $acl
140
     */
141
    public function head($module, $controller, $acl = null)
142
    {
143
        $this->addMap(RequestMethod::HEAD, $module, $controller, $acl);
144
    }
145
146
    /**
147
     * Add mapping for GET method
148
     *
149
     * @param string $module
150
     * @param string $controller
151
     * @param String $acl
152
     */
153
    public function get($module, $controller, $acl = null)
154
    {
155
        $this->addMap(RequestMethod::GET, $module, $controller, $acl);
156
    }
157
158
    /**
159
     * Add mapping for POST method
160
     *
161
     * @param string $module
162
     * @param string $controller
163
     * @param String $acl
164
     */
165
    public function post($module, $controller, $acl = null)
166
    {
167
        $this->addMap(RequestMethod::POST, $module, $controller, $acl);
168
    }
169
170
    /**
171
     * Add mapping for PATCH method
172
     *
173
     * @param string $module
174
     * @param string $controller
175
     * @param String $acl
176
     */
177
    public function patch($module, $controller, $acl = null)
178
    {
179
        $this->addMap(RequestMethod::PATCH, $module, $controller, $acl);
180
    }
181
182
    /**
183
     * Add mapping for PUT method
184
     *
185
     * @param string $module
186
     * @param string $controller
187
     * @param String $acl
188
     */
189
    public function put($module, $controller, $acl = null)
190
    {
191
        $this->addMap(RequestMethod::PUT, $module, $controller, $acl);
192
    }
193
194
    /**
195
     * Add mapping for DELETE method
196
     *
197
     * @param string $module
198
     * @param string $controller
199
     * @param String $acl
200
     */
201
    public function delete($module, $controller, $acl = null)
202
    {
203
        $this->addMap(RequestMethod::DELETE, $module, $controller, $acl);
204
    }
205
206
    /**
207
     * Add mapping for OPTIONS method
208
     *
209
     * @param string $module
210
     * @param string $controller
211
     * @param String $acl
212
     */
213
    public function options($module, $controller, $acl = null)
214
    {
215
        $this->addMap(RequestMethod::OPTIONS, $module, $controller, $acl);
216
    }
217
218
    /**
219
     * Set Crud
220
     *
221
     * @param AbstractCrud $crud
222
     */
223 10
    public function setCrud($crud)
224
    {
225 10
        $this->crud = $crud;
226 10
    }
227
228
    /**
229
     * Get crud instance
230
     *
231
     * @return AbstractCrud
232
     * @throws ControllerException
233
     */
234 10
    public function getCrud()
235
    {
236 10
        if (!$this->crud) {
237 1
            throw new ControllerException('`Crud` class is not exists or not initialized');
238
        }
239 9
        return $this->crud;
240
    }
241
242
    /**
243
     * Return primary key
244
     *
245
     * @return array
246
     */
247 8
    public function getPrimaryKey()
248
    {
249 8
        if (is_null($this->primary)) {
250 8
            $primary = $this->getCrud()->getPrimaryKey();
251 7
            $this->primary = array_intersect_key($this->data, array_flip($primary));
252
        }
253 7
        return $this->primary;
254
    }
255
256
    /**
257
     * Run REST controller
258
     * @return mixed
259
     * @throws ForbiddenException
260
     * @throws NotImplementedException
261
     */
262 6
    public function run()
263
    {
264
        // check implementation
265 6
        if (!isset($this->map[$this->method])) {
266
            throw new NotImplementedException;
267
        }
268
269 6
        $map = $this->map[$this->method];
270
271
        // check permissions
272 6
        if (isset($map['acl'])) {
273 1
            if (!Acl::isAllowed($this->module, $map['acl'])) {
274 1
                throw new ForbiddenException;
275
            }
276
        }
277
278
        // dispatch controller
279 5
        return Application::getInstance()->dispatch(
280 5
            $map['module'],
281 5
            $map['controller'],
282
            [
283 5
                'crud' => $this->getCrud(),
284 5
                'primary' => $this->getPrimaryKey(),
285 5
                'data' => $this->data
286
            ]
287
        );
288
    }
289
}
290