Passed
Pull Request — master (#65)
by Rafael
05:14
created

AclMiddleware::call()   B

Complexity

Conditions 10
Paths 15

Size

Total Lines 50
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 63.6376

Importance

Changes 0
Metric Value
cc 10
eloc 32
nc 15
nop 1
dl 0
loc 50
ccs 6
cts 32
cp 0.1875
crap 63.6376
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Middleware;
6
7
use Phalcon\Mvc\Micro;
8
use Phalcon\Mvc\Micro\MiddlewareInterface;
9
use Gewaer\Exception\ServerErrorHttpException;
10
use Gewaer\Exception\PermissionException;
11
use Gewaer\Models\Subscription;
12
13
/**
14
 * Class AclMiddleware
15
 *
16
 * @package Gewaer\Middleware
17
 */
18
class AclMiddleware implements MiddlewareInterface
19
{
20
    /**
21
     * Call me
22
     *
23
     * @param Micro $api
24
     * @todo need to check section for auth here
25
     * @return bool
26
     */
27 69
    public function call(Micro $api)
28
    {
29 69
        $auth = $api->getService('auth');
30 69
        $router = $api->getService('router');
31 69
        $request = $api->getService('request');
32
33 69
        if (!$auth->isIgnoreUri()) {
34
            // explode() by / , postiion #1 is always the controller , so its the resource ^.^
35
            $matchRouter = explode('/', $router->getMatchedRoute()->getCompiledPattern());
36
            $resource = ucfirst($matchRouter[2]); //2 is alwasy the controller of the router
37
            $userData = $api->getService('userData');
38
39
            $action = null;
40
            // GET -> read
41
            // PUT -> update
42
            // DELETE -> delete
43
            // POST -> create
44
45
            if (!Subscription::getPaymentStatus()) {
46
                throw new ServerErrorHttpException('Subscription is not active.Please contact your admin');
47
            }
48
49
            switch (strtolower($request->getMethod())) {
50
                case 'get':
51
                    $action = 'list';
52
                    if (preg_match("/\/([0-9]+)(?=[^\/]*$)/", $request->getURI())) {
53
                        $action = 'read';
54
                    }
55
                    break;
56
                case 'post':
57
                    $action = 'create';
58
                    break;
59
                case 'delete':
60
                    $action = 'delete';
61
                    break;
62
                case 'put':
63
                case 'patch':
64
                    $action = 'update';
65
                    break;
66
                default:
67
                    throw new ServerErrorHttpException('No Permission define for this action');
68
                break;
69
            }
70
            //do you have permision
71
            if (!$userData->can($resource . '.' . $action)) {
72
                throw new PermissionException('You dont have permission to run this action ' . $action . ' at ' . $resource);
73
            }
74
        }
75
76 69
        return true;
77
    }
78
}
79