Completed
Push — master ( 22fa00...44c771 )
by ARCANEDEV
07:45
created

ViewComposer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 77
ccs 0
cts 11
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCachedUsers() 0 6 1
A getCachedRoles() 0 6 1
A getCachedPermissions() 0 6 1
A cacheResults() 0 4 1
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 Functions
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::all();
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 Functions
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