Completed
Push — master ( ffb215...f806ab )
by Anton
11s
created

AbstractMapper   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
c 5
b 1
f 2
dl 0
loc 249
ccs 0
cts 84
cp 0
rs 10
wmc 17
lcom 1
cbo 8

12 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 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
     * Set Crud
209
     *
210
     * @param AbstractCrud $crud
211
     */
212
    public function setCrud($crud)
213
    {
214
        $this->crud = $crud;
215
    }
216
217
    /**
218
     * Get crud instance
219
     *
220
     * @return AbstractCrud
221
     * @throws ControllerException
222
     */
223
    public function getCrud()
224
    {
225
        if (!$this->crud) {
226
            throw new ControllerException("`Crud` class is not exists or not initialized");
227
        }
228
        return $this->crud;
229
    }
230
231
    /**
232
     * Return primary key
233
     *
234
     * @return array
235
     */
236
    public function getPrimaryKey()
237
    {
238
        if (is_null($this->primary)) {
239
            $primary = $this->getCrud()->getPrimaryKey();
240
            $this->primary = array_intersect_key($this->data, array_flip($primary));
241
        }
242
        return $this->primary;
243
    }
244
245
    /**
246
     * Run REST controller
247
     * @return mixed
248
     * @throws ForbiddenException
249
     * @throws NotImplementedException
250
     */
251
    public function run()
252
    {
253
        // check implementation
254
        if (!isset($this->map[$this->method])) {
255
            throw new NotImplementedException;
256
        }
257
258
        $map = $this->map[$this->method];
259
260
        // check permissions
261
        if (isset($map['acl'])) {
262
            if (!Acl::isAllowed($this->module, $map['acl'])) {
263
                throw new ForbiddenException;
264
            }
265
        }
266
267
        // dispatch controller
268
        return Application::getInstance()->dispatch(
269
            $map['module'],
270
            $map['controller'],
271
            [
272
                'crud' => $this->getCrud(),
273
                'primary' => $this->getPrimaryKey(),
274
                'data' => $this->data
275
            ]
276
        );
277
    }
278
}
279