Completed
Push — master ( d6976d...321e84 )
by Mostafa Abd El-Salam
17s queued 11s
created

Helpers::checkVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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

125
        return ! (stripos(app()->/** @scrutinizer ignore-call */ version(), 'lumen') !== false);
Loading history...
126
    }
127
128
    /**
129
     * @return bool
130
     */
131
    public function checkVersion(): bool
132
    {
133
        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...
134
    }
135
}
136