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

AclMiddleware::call()   B

Complexity

Conditions 10
Paths 15

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 66.8897

Importance

Changes 0
Metric Value
cc 10
eloc 35
nc 15
nop 1
dl 0
loc 57
ccs 6
cts 35
cp 0.1714
crap 66.8897
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    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\CompaniesSettings;
12
use Phalcon\Di;
13
14
/**
15
 * Class AclMiddleware
16
 *
17
 * @package Gewaer\Middleware
18
 */
19
class AclMiddleware implements MiddlewareInterface
20
{
21
    /**
22
     * Call me
23
     *
24
     * @param Micro $api
25
     * @todo need to check section for auth here
26
     * @return bool
27
     */
28 69
    public function call(Micro $api)
29
    {
30 69
        $auth = $api->getService('auth');
31 69
        $router = $api->getService('router');
32 69
        $request = $api->getService('request');
33
34 69
        if (!$auth->isIgnoreUri()) {
35
            // explode() by / , postiion #1 is always the controller , so its the resource ^.^
36
            $matchRouter = explode('/', $router->getMatchedRoute()->getCompiledPattern());
37
            $resource = ucfirst($matchRouter[2]); //2 is alwasy the controller of the router
38
            $userData = $api->getService('userData');
39
40
            $action = null;
41
            // GET -> read
42
            // PUT -> update
43
            // DELETE -> delete
44
            // POST -> create
45
46
            //Search current company's app setting with key paid to verify payment status for current company
47
            $subscriptionPaid = CompaniesSettings::findFirst([
48
                'conditions' => "companies_id = ?0 and name = 'paid' and is_deleted = 0",
49
                'bind' => [Di::getDefault()->getUserData()->default_company]
50
            ]);
51
52
            if (!$subscriptionPaid->value) {
53
                throw new ServerErrorHttpException('Subscription is not active.Please contact your admin');
54
            }
55
56
            switch (strtolower($request->getMethod())) {
57
                case 'get':
58
                    $action = 'list';
59
                    if (preg_match("/\/([0-9]+)(?=[^\/]*$)/", $request->getURI())) {
60
                        $action = 'read';
61
                    }
62
                    break;
63
                case 'post':
64
                    $action = 'create';
65
                    break;
66
                case 'delete':
67
                    $action = 'delete';
68
                    break;
69
                case 'put':
70
                case 'patch':
71
                    $action = 'update';
72
                    break;
73
                default:
74
                    throw new ServerErrorHttpException('No Permission define for this action');
75
                break;
76
            }
77
78
            //do you have permision
79
            if (!$userData->can($resource . '.' . $action)) {
80
                throw new PermissionException('You dont have permission to run this action ' . $action . ' at ' . $resource);
81
            }
82
        }
83
84 69
        return true;
85
    }
86
}
87