Test Failed
Pull Request — master (#80)
by Maximo
05:41
created

AclMiddleware::call()   B

Complexity

Conditions 11
Paths 15

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 11.2597

Importance

Changes 0
Metric Value
cc 11
eloc 31
nc 15
nop 1
dl 0
loc 50
ccs 27
cts 31
cp 0.871
crap 11.2597
rs 7.3166
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 extends TokenBase
19
{
20
    /**
21
     * Call me.
22
     *
23
     * @param Micro $api
24
     * @todo need to check section for auth here
25
     * @return bool
26
     */
27 68
    public function call(Micro $api)
28
    {
29 68
        $router = $api->getService('router');
30 68
        $request = $api->getService('request');
31
32 68
        if ($this->isValidCheck($request)) {
33
            // explode() by / , postiion #1 is always the controller , so its the resource ^.^
34 65
            $matchRouter = explode('/', $router->getMatchedRoute()->getCompiledPattern());
35
36 65
            $resource = ucfirst(isset($matchRouter[2]) ? $matchRouter[2] : $matchRouter[1]); //2 is alwasy the controller of the router
37 65
            $userData = $api->getService('userData');
38
39 65
            $action = null;
40
            // GET -> read
41
            // PUT -> update
42
            // DELETE -> delete
43
            // POST -> create
44
45 65
            if (!Subscription::getPaymentStatus()) {
46
                throw new ServerErrorHttpException('Subscription is not active.Please contact your admin');
47
            }
48
49 65
            switch (strtolower($request->getMethod())) {
50 65
                case 'get':
51 39
                    $action = 'list';
52 39
                    if (preg_match("/\/([0-9]+)(?=[^\/]*$)/", $request->getURI())) {
53 7
                        $action = 'read';
54
                    }
55 39
                    break;
56 47
                case 'post':
57 25
                    $action = 'create';
58 25
                    break;
59 22
                case 'delete':
60 8
                    $action = 'delete';
61 8
                    break;
62 14
                case 'put':
63
                case 'patch':
64 14
                    $action = 'update';
65 14
                    break;
66
                default:
67
                    throw new ServerErrorHttpException('No Permission define for this action');
68
                break;
69
            }
70
            //do you have permision
71 65
            if (!$userData->can($resource . '.' . $action)) {
72 1
                throw new PermissionException('You dont have permission to run this action ' . $action . ' at ' . $resource);
73
            }
74
        }
75
76 68
        return true;
77
    }
78
}
79