Passed
Branch v1.3 (89ade3)
by Mostafa Abd El-Salam
03:08
created

Helpers::abort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 1.037
1
<?php
2
declare(strict_types=1);
3
4
namespace Maklad\Permission;
5
6
use Illuminate\Container\Container;
7
use Illuminate\Support\Collection;
8
9
/**
10
 * Class Helpers
11
 * @package Maklad\Permission
12
 */
13
class Helpers
14
{
15
    /**
16
     * @param string $guard
17
     *
18
     * @return string|null
19
     */
20 4
    public function getModelForGuard(string $guard)
21
    {
22 4
        $guards = new Collection($this->config('auth.guards'));
23 4
        return $guards->map(function ($guard) {
24 4
            return $this->config("auth.providers.{$guard['provider']}.model");
25 4
        })->get($guard);
26
    }
27
28
    /**
29
     * @param Collection $expected
30
     * @param string $given
31
     *
32
     * @return string
33
     */
34 6
    public function getGuardDoesNotMatchMessage(Collection $expected, string $given): string
35
    {
36 6
        return "The given role or permission should use guard `{$expected->implode(', ')}` instead of `{$given}`.";
37
    }
38
39
    /**
40
     * @param string $name
41
     * @param string $guardName
42
     *
43
     * @return string
44
     */
45 1
    public function getPermissionAlreadyExistsMessage(string $name, string $guardName): string
46
    {
47 1
        return "A permission `{$name}` already exists for guard `{$guardName}`.";
48
    }
49
50
    /**
51
     * @param string $name
52
     * @param string $guardName
53
     *
54
     * @return string
55
     */
56 7
    public function getPermissionDoesNotExistMessage(string $name, string $guardName): string
57
    {
58 7
        return "There is no permission named `{$name}` for guard `{$guardName}`.";
59
    }
60
61
    /**
62
     * @param string $name
63
     * @param string $guardName
64
     *
65
     * @return string
66
     */
67 1
    public function getRoleAlreadyExistsMessage(string $name, string $guardName): string
68
    {
69 1
        return "A role `{$name}` already exists for guard `{$guardName}`.";
70
    }
71
72
    /**
73
     * @param string $name
74
     *
75
     * @param string $guardName
76
     *
77
     * @return string
78
     */
79 2
    public function getRoleDoesNotExistMessage(string $name, string $guardName): string
80
    {
81 2
        return "There is no role named `{$name}` for guard `{$guardName}`.";
82
    }
83
84
    /**
85
     * @param null|string|array $key
86
     * @param null $default
87
     *
88
     * @return mixed|static
89
     */
90 110
    public function config($key = null, $default = null)
91
    {
92 110
        if (null === $key) {
93
            return $this->app('config');
94
        }
95
96 110
        if (\is_array($key)) {
97
            return $this->app('config')->set($key);
98
        }
99
100 110
        return $this->app('config')->get($key, $default);
101
    }
102
103
    /**
104
     * @param null|string|array $abstract
105
     * @param array $parameters
106
     *
107
     * @return mixed|static
108
     */
109 110
    public function app($abstract = null, array $parameters = [])
110
    {
111 110
        if (null === $abstract) {
112 7
            return Container::getInstance();
113
        }
114
115 110
        return empty($parameters)
116 110
            ? Container::getInstance()->make($abstract)
0 ignored issues
show
Bug introduced by
It seems like $abstract defined by parameter $abstract on line 109 can also be of type array; however, Illuminate\Container\Container::make() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
117 110
            : Container::getInstance()->makeWith($abstract, $parameters);
0 ignored issues
show
Bug introduced by
It seems like $abstract defined by parameter $abstract on line 109 can also be of type array; however, Illuminate\Container\Container::makeWith() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
118
    }
119
120
    /**
121
     * @param $code
122
     * @param string $message
123
     * @param array $headers
124
     */
125 7
    public function abort($code, $message = '', array $headers = [])
126
    {
127 7
        $this->app()->abort($code, $message, $headers);
128
    }
129
}
130