Completed
Pull Request — master (#2)
by ARCANEDEV
03:02
created

DashboardComposer::cacheResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php namespace Arcanesoft\Auth\ViewComposers;
2
3
use Arcanesoft\Auth\Bases\ViewComposer;
4
use Arcanesoft\Auth\Models\Permission;
5
use Arcanesoft\Auth\Models\Role;
6
use Arcanesoft\Auth\Models\User;
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 extends ViewComposer
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Properties
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /**
22
     * @var \Arcanesoft\Auth\Models\User
23
     */
24
    protected $users;
25
26
    /**
27
     * @var \Arcanesoft\Auth\Models\Role
28
     */
29
    protected $roles;
30
31
    /**
32
     * @var \Arcanesoft\Auth\Models\Permission
33
     */
34
    protected $permissions;
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Constructor
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * DashboardComposer constructor.
42
     *
43
     * @param User $users
44
     */
45
    public function __construct(User $users, Role $roles, Permission $permissions)
46
    {
47
        $this->users       = $users;
48
        $this->roles       = $roles;
49
        $this->permissions = $permissions;
50
    }
51
52
    /* ------------------------------------------------------------------------------------------------
53
     |  Main Functions
54
     | ------------------------------------------------------------------------------------------------
55
     */
56
    /**
57
     * Compose the view.
58
     *
59
     * @param  \Illuminate\Contracts\View\View  $view
60
     */
61
    public function compose(View $view)
62
    {
63
        $this->view = $view;
64
65
        $this->composeUsersTotal();
66
        $this->composeRolesTotal();
67
        $this->composePermissionsTotal();
68
    }
69
70
    protected function composeUsersTotal()
71
    {
72
        $count = $this->cacheResults('auth-users-count', function() {
73
            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...
74
        });
75
76
        $this->view->with('authUsersCount', $count);
77
    }
78
79
    protected function composeRolesTotal()
80
    {
81
        $count = $this->cacheResults('auth-roles-count', function() {
82
            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...
83
        });
84
85
        $this->view->with('authRolesCount', $count);
86
    }
87
88
    protected function composePermissionsTotal()
89
    {
90
        $count = $this->cacheResults('auth-permissions-count', function() {
91
            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...
92
        });
93
94
        $this->view->with('authPermissionsCount', $count);
95
    }
96
}
97