Passed
Push — master ( fa07c5...26ce7c )
by Al Amin
06:02
created

Permission::scopeOfRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace PhpCollective\MenuMaker\Storage;
4
5
use Cache;
6
use Illuminate\Database\Eloquent\Builder;
7
use Route;
8
use Illuminate\Database\Eloquent\Model;
9
10
class Permission extends Model
11
{
12
13
    /**
14
     * The table associated with the model.
15
     *
16
     * @var string
17
     */
18
    protected $table = 'pcmm_permissions';
19
20
    /**
21
     * Attributes that should be mass-assignable.
22
     *
23
     * @var array
24
     */
25
    protected $fillable = [
26
        'menu_id',
27
        'namespace',
28
        'controller',
29
        'method',
30
        'action',
31
        'allowed'
32
    ];
33
34
    /**
35
     * The group's default attributes.
36
     *
37
     * @var array
38
     */
39
    protected $attributes = [
40
        'allowed' => false
41
    ];
42
43
    /**
44
     * The attributes that should be cast to native types.
45
     *
46
     * @var array
47
     */
48
    protected $casts = [
49
        'allowed' => 'integer'
50
    ];
51
52
    /**
53
     * Get the menu of the permission.
54
     */
55
    public function menu()
56
    {
57
        return $this->belongsTo(Menu::class);
58
    }
59
60
61
    public static function routes()
62
    {
63
        return Cache::remember('routes', now()->addHour(), function () {
64
            $filterRoutes = [];
65
            $routes = Route::getRoutes();
66
            foreach ($routes as $route) {
67
                if (! self::routeMatch($route)) {
68
                    continue;
69
                }
70
                $filterRoutes[] = explode_route($route);
71
            }
72
            return collect($filterRoutes);
73
        });
74
    }
75
76
    public static function excludedActionList()
77
    {
78
        return Cache::rememberForever('excluded-action-list', function () {
79
            $excluded = collect();
80
            $routes = Route::getRoutes();
81
            foreach ($routes as $route) {
82
                if (is_working_route($route) && is_excluded_route($route)) {
83
                    $excluded->push($route->getActionName());
84
                }
85
            }
86
            return $excluded;
87
        });
88
    }
89
90
    public static function publicRoutes()
91
    {
92
        return Cache::rememberForever('public-routes', function () {
93
            return collect(self::public()->get()->toArray());
94
        });
95
    }
96
97
    public static function actions()
98
    {
99
        return Cache::rememberForever('route-actions', function () {
100
            $actions = [];
101
            self::routes()->each(function ($route) use (&$actions) {
102
                $actions[$route['namespace']][$route['controller']][$route['method']][] = $route['action'];
103
            });
104
            ksort($actions);
105
            return $actions;
106
        });
107
    }
108
109
    private static function routeMatch($route)
110
    {
111
        return is_working_route($route) && ! is_excluded_route($route);
112
    }
113
114
    public function scopeOfRoute(Builder $query, $route)
115
    {
116
        return $query->where(function ($query) use ($route) {
117
            $query->where('namespace', $route['namespace'])
118
                ->where('controller', $route['controller'])
119
                ->where('method', $route['method'])
120
                ->where('action', $route['action']);
121
        });
122
    }
123
124
    public function scopePublic(Builder $query)
125
    {
126
        return $query->select('pcmm_permissions.*')
127
            ->leftJoin('pcmm_menus', 'pcmm_menus.id', '=', 'pcmm_permissions.menu_id')
128
            ->where('pcmm_menus.privilege', 'PUBLIC')
129
            ->where('pcmm_permissions.method', 'GET');
130
    }
131
}
132