ViewComposer::getCachedPermissions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Auth\ViewComposers;
2
3
use Arcanesoft\Auth\Models\Permission;
4
use Arcanesoft\Auth\Models\Role;
5
use Arcanesoft\Auth\Models\User;
6
use Closure;
7
use Illuminate\Support\Facades\Cache;
8
9
/**
10
 * Class     ViewComposer
11
 *
12
 * @package  Arcanesoft\Auth\Bases
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
abstract class ViewComposer
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
    /**
22
     * The View instance.
23
     *
24
     * @var \Illuminate\Contracts\View\View
25
     */
26
    protected $view;
27
28
    /**
29
     * Caching time.
30
     *
31
     * @var int
32
     */
33
    protected $minutes = 5;
34
35
    /* -----------------------------------------------------------------
36
     |  Main Methods
37
     | -----------------------------------------------------------------
38
     */
39
    /**
40
     * Get all cached users.
41
     *
42
     * @return \Illuminate\Database\Eloquent\Collection
43
     */
44
    public function getCachedUsers()
45
    {
46
        return $this->cacheResults('users.all', function() {
47
            return User::protectAdmins()->get();
0 ignored issues
show
Bug introduced by
The method protectAdmins() does not exist on Arcanesoft\Auth\Models\User. Did you maybe mean scopeProtectAdmins()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
48
        });
49
    }
50
51
    /**
52
     * Get all cached roles.
53
     *
54
     * @return \Illuminate\Database\Eloquent\Collection
55
     */
56
    public function getCachedRoles()
57
    {
58
        return $this->cacheResults('roles.all', function() {
59
            return Role::all();
60
        });
61
    }
62
63
    /**
64
     * Get all cached permissions.
65
     *
66
     * @return \Illuminate\Database\Eloquent\Collection
67
     */
68
    protected function getCachedPermissions()
69
    {
70
        return $this->cacheResults('permissions.all', function() {
71
            return Permission::all();
72
        });
73
    }
74
75
    /* -----------------------------------------------------------------
76
     |  Other Methods
77
     | -----------------------------------------------------------------
78
     */
79
    /**
80
     * Cache the results.
81
     *
82
     * @param  string    $name
83
     * @param  \Closure  $callback
84
     *
85
     * @return mixed
86
     */
87
    protected function cacheResults($name, Closure $callback)
88
    {
89
        return Cache::remember("auth::{$name}", $this->minutes, $callback);
90
    }
91
}
92