Helpers::getPermissionDoesNotExistMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Maklad\Permission;
4
5
use Illuminate\Support\Collection;
6
use function collect;
7
use function config;
8
9
/**
10
 * Class Helpers
11
 * @package Maklad\Permission
12
 */
13
class Helpers
14
{
15
    /**
16
     * @param string $guard
17
     *
18 4
     * @return string|null
19
     */
20 4
    public function getModelForGuard(string $guard): ?string
21 4
    {
22 4
        return collect(config('auth.guards'))
23 4
            ->map(function ($guard) {
24
                return config("auth.providers.{$guard['provider']}.model");
25
            })->get($guard);
26
    }
27
28
    /**
29
     * @param Collection $expected
30
     * @param string $given
31
     *
32 6
     * @return string
33
     */
34 6
    public function getGuardDoesNotMatchMessage(Collection $expected, string $given): string
35
    {
36
        $expectedStr = $expected->implode(', ');
37
        return "The given role or permission should use guard `$expectedStr` instead of `$given`.";
38
    }
39
40
    /**
41
     * @param string $name
42
     * @param string $guardName
43 1
     *
44
     * @return string
45 1
     */
46
    public function getPermissionAlreadyExistsMessage(string $name, string $guardName): string
47
    {
48
        return "A permission `$name` already exists for guard `$guardName`.";
49
    }
50
51
    /**
52
     * @param string $name
53
     * @param string $guardName
54 7
     *
55
     * @return string
56 7
     */
57
    public function getPermissionDoesNotExistMessage(string $name, string $guardName): string
58
    {
59
        return "There is no permission named `$name` for guard `$guardName`.";
60
    }
61
62
    /**
63
     * @param string $name
64
     * @param string $guardName
65 1
     *
66
     * @return string
67 1
     */
68
    public function getRoleAlreadyExistsMessage(string $name, string $guardName): string
69
    {
70
        return "A role `$name` already exists for guard `$guardName`.";
71
    }
72
73
    /**
74
     * @param string $name
75
     *
76
     * @param string $guardName
77 2
     *
78
     * @return string
79 2
     */
80
    public function getRoleDoesNotExistMessage(string $name, string $guardName): string
81
    {
82
        return "There is no role named `$name` for guard `$guardName`.";
83
    }
84
85
    /**
86
     * @param string $roles
87 4
     *
88
     * @return string
89 4
     */
90 4
    public function getUnauthorizedRoleMessage(string $roles): string
91 4
    {
92
        $message = "User does not have the right roles `$roles`.";
93
        if (! config('permission.display_permission_in_exception')) {
94 4
            $message = 'User does not have the right roles.';
95
        }
96
97
        return $message;
98
    }
99
100
    /**
101
     * @param string $permissions
102 3
     *
103
     * @return string
104 3
     */
105 3
    public function getUnauthorizedPermissionMessage(string $permissions): string
106 3
    {
107
        $message = "User does not have the right permissions `$permissions`.";
108
        if (! config('permission.display_permission_in_exception')) {
109 3
            $message = 'User does not have the right permissions.';
110
        }
111
112
        return $message;
113
    }
114
115 2
    /**
116
     * @return string
117 2
     */
118
    public function getUserNotLoggedINMessage(): string
119
    {
120 123
        return 'User is not logged in.';
121
    }
122 123
123
    /**
124
     * @return bool
125
     */
126
    public function isNotLumen(): bool
127
    {
128
        return ! (stripos(app()->version(), 'lumen') !== false);
0 ignored issues
show
introduced by
The method version() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

128
        return ! (stripos(app()->/** @scrutinizer ignore-call */ version(), 'lumen') !== false);
Loading history...
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    public function checkVersion(): bool
135
    {
136
        return ($this->isNotLumen() && app()::VERSION < '5.4');
0 ignored issues
show
Bug introduced by
The constant Illuminate\Container\Container::VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
137
    }
138
139
    /**
140
     * @param array $items
141
     * @return array
142
     */
143
    public function flattenArray(array $items): array
144
    {
145
        return collect($items)->map(function ($item) {
0 ignored issues
show
Bug introduced by
$items of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

145
        return collect(/** @scrutinizer ignore-type */ $items)->map(function ($item) {
Loading history...
146
            return is_string($item) ? explode('|', $item): $item;
147
        })->flatten()->all();
148
    }
149
}
150