MenuObserver::removeUserCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace PhpCollective\MenuMaker\Observers;
4
5
6
use Illuminate\Support\Facades\Cache;
7
use PhpCollective\MenuMaker\Storage\Menu;
8
use PhpCollective\MenuMaker\Storage\Role;
9
use PhpCollective\MenuMaker\Jobs\RemoveUserMenuCache;
10
11
class MenuObserver
12
{
13
14
    /**
15
     * Handle the User "created" event.
16
     *
17
     * @param  \App\Menu  $menu
0 ignored issues
show
Bug introduced by
The type App\Menu was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
     * @return void
19
     */
20
    public function created(Menu $menu)
0 ignored issues
show
Unused Code introduced by
The parameter $menu is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

20
    public function created(/** @scrutinizer ignore-unused */ Menu $menu)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
21
    {
22
        $this->removeAdminCache();
23
    }
24
25
    /**
26
     * Handle the Menu "updated" event.
27
     *
28
     * @param  \App\Menu  $menu
29
     * @return void
30
     */
31
    public function updated(Menu $menu)
32
    {
33
        $this->removeAdminCache();
34
        $this->removeUserCache($menu);
35
    }
36
37
    /**
38
     * Handle the Menu "deleting" event.
39
     *
40
     * @param  Menu  $menu
41
     * @return void
42
     */
43
    public function deleting(Menu $menu)
44
    {
45
        $this->removeAdminCache();
46
        $this->removeUserCache($menu);
47
    }
48
49
    /**
50
     * Handle User Cache delete.
51
     *
52
     * @param  Menu  $menu
53
     * @return void
54
     */
55
    private function removeUserCache(Menu $menu)
56
    {
57
        $menu->load('roles', 'roles.users');
58
        $menu->roles->each(function ($role) {
59
            $role->users->each(function ($user) {
60
                RemoveUserMenuCache::dispatch($user);
61
            });
62
        });
63
    }
64
65
    /**
66
     * Handle Admin User Cache delete.
67
     *
68
     * @return void
69
     */
70
    private function removeAdminCache()
71
    {
72
        Cache::forget('public-routes');
73
74
        $admin = Role::with('users')->admin()->first();
75
        if(! $admin)
76
        {
77
            return;
78
        }
79
80
        $admin->users->each(function ($user) {
0 ignored issues
show
Bug introduced by
The property users does not seem to exist on PhpCollective\MenuMaker\Storage\Role. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
81
            RemoveUserMenuCache::dispatch($user);
82
        });
83
    }
84
}
85