Completed
Push — master ( e43e24...a5d827 )
by ARCANEDEV
05:31
created

DashboardComposer::composePermissionsTotal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
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\Contracts\View\View;
8
9
/**
10
 * Class     DashboardComposer
11
 *
12
 * @package  Arcanesoft\Auth\ViewComposers
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class DashboardComposer
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Properties
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /**
22
     * @var \Illuminate\Contracts\View\View
23
     */
24
    protected $view;
25
26
    /**
27
     * @var \Arcanesoft\Auth\Models\User
28
     */
29
    protected $users;
30
31
    /**
32
     * @var \Arcanesoft\Auth\Models\Role
33
     */
34
    protected $roles;
35
36
    /**
37
     * @var \Arcanesoft\Auth\Models\Permission
38
     */
39
    protected $permissions;
40
41
    /**
42
     * @var int
43
     */
44
    protected $minutes = 5;
45
46
    /* ------------------------------------------------------------------------------------------------
47
     |  Constructor
48
     | ------------------------------------------------------------------------------------------------
49
     */
50
    /**
51
     * DashboardComposer constructor.
52
     *
53
     * @param User $users
54
     */
55
    public function __construct(User $users, Role $roles, Permission $permissions)
56
    {
57
        $this->users       = $users;
58
        $this->roles       = $roles;
59
        $this->permissions = $permissions;
60
    }
61
62
    /* ------------------------------------------------------------------------------------------------
63
     |  Main Functions
64
     | ------------------------------------------------------------------------------------------------
65
     */
66
    /**
67
     * Compose the view.
68
     *
69
     * @param  \Illuminate\Contracts\View\View  $view
70
     */
71
    public function compose(View $view)
72
    {
73
        $this->view = $view;
74
75
        $this->composeUsersTotal();
76
        $this->composeRolesTotal();
77
        $this->composePermissionsTotal();
78
    }
79
80
    protected function composeUsersTotal()
81
    {
82
        $total = $this->cacheResults('auth-users-count', function() {
83
            return $this->users->count();
0 ignored issues
show
Documentation Bug introduced by
The method count does not exist on object<Arcanesoft\Auth\Models\User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
84
        });
85
86
        $this->view->with('authUsersTotal', $total);
87
    }
88
89
    protected function composeRolesTotal()
90
    {
91
        $total = $this->cacheResults('auth-roles-count', function() {
92
            return $this->roles->count();
0 ignored issues
show
Documentation Bug introduced by
The method count does not exist on object<Arcanesoft\Auth\Models\Role>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
93
        });
94
95
        $this->view->with('authRolesTotal', $total);
96
    }
97
98
    protected function composePermissionsTotal()
99
    {
100
        $total = $this->cacheResults('auth-permissions-count', function() {
101
            return $this->permissions->count();
0 ignored issues
show
Documentation Bug introduced by
The method count does not exist on object<Arcanesoft\Auth\Models\Permission>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
102
        });
103
104
        $this->view->with('authPermissionsTotal', $total);
105
    }
106
107
    /**
108
     * Cache the results.
109
     *
110
     * @param  string    $name
111
     * @param  \Closure  $callback
112
     *
113
     * @return mixed
114
     */
115
    protected function cacheResults($name, Closure $callback)
116
    {
117
        return \Cache::remember($name, $this->minutes, $callback);
118
    }
119
}
120