Completed
Push — master ( 8def13...f97cbb )
by Anton
8s
created

AbstractMapper   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 1 Features 3
Metric Value
c 6
b 1
f 3
dl 0
loc 261
ccs 0
cts 88
cp 0
rs 10
wmc 18
lcom 1
cbo 8

13 Methods

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