Passed
Push — master ( b4b8e8...3089dd )
by Arthur
26:17 queued 02:28
created

get_authenticated_user()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 2
cts 3
cp 0.6667
crap 2.1481
rs 10
c 0
b 0
f 0
1
<?php
2
3
if (!function_exists('get_module_path')) {
4
    function get_module_path(string $module)
5
    {
6
        $module = ucfirst($module);
7
        $path = base_path('src/Modules') . '/' . $module;
8
        if (file_exists($path)) {
9
            return $path;
10
        }
11
        throw new \Foundation\Exceptions\Exception("Module not found", 500);
12
    }
13
}
14
15
if (!function_exists('get_foundation_path')) {
16
    function get_foundation_path()
17
    {
18
        return base_path('src/Foundation');
19
    }
20
}
21
22
23
if (!function_exists('get_authenticated_user_id')) {
24
    function get_authenticated_user_id()
25
    {
26 14
        return get_authenticated_user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
27
    }
28
}
29
30
if (!function_exists('get_authenticated_user')) {
31
32
    /**
33
     * @return \Modules\User\Entities\User
34
     */
35
    function get_authenticated_user(): \Illuminate\Contracts\Auth\Authenticatable
36
    {
37 19
        if (Auth::user() !== null) {
38 19
            return Auth::user();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Auth::user() could return the type null which is incompatible with the type-hinted return Illuminate\Contracts\Auth\Authenticatable. Consider adding an additional type-check to rule them out.
Loading history...
39
        } else {
40
            throw new \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException('no authorized user');
41
        }
42
    }
43
}
44
45
if (!function_exists('get_short_class_name')) {
46
    function get_short_class_name($class)
47
    {
48 90
        if (!is_string($class)) {
49 90
            $class = get_class($class);
50
        }
51
52 90
        return substr(strrchr($class, '\\'), 1);
53
    }
54
}
55
56
if (!function_exists('get_random_array_element')) {
57
    function get_random_array_element(array $array)
58
    {
59 77
        if (empty($array)) {
60
            return;
61
        }
62 77
        $randomIndex = random_int(0, count($array) - 1);
63
64 77
        return $array[$randomIndex];
65
    }
66
}
67
if (!function_exists('create_multiple_from_factory')) {
68
    function create_multiple_from_factory(string $modelClass, $amount = 1, ?string $state = null)
69
    {
70
        if ($amount < 1) {
71
            return false;
72
        }
73
74
        $factory = factory($modelClass, $amount);
75
76
        if ($state !== null) {
77
            $factory->state($state);
78
        }
79
80
        return $factory->raw();
81
    }
82
}
83
84
if (!function_exists('create_from_factory')) {
85
    function create_from_factory(string $modelClass, ?string $state = null)
86
    {
87
        $factory = factory($modelClass);
88
89
        if ($state !== null) {
90
            $factory->state($state);
91
        }
92
93
        return $factory->raw();
94
    }
95
}
96
97
if (!function_exists('class_implements_interface')) {
98
    function class_implements_interface($class, $interface)
99
    {
100 90
        return in_array($interface, class_implements($class));
101
    }
102
}
103
104
if (!function_exists('class_uses_trait')) {
105
    function class_uses_trait($class, string $trait)
106
    {
107 90
        if (!is_string($class)) {
108
            $class = get_class($class);
109
        }
110
111 90
        $traits = array_flip(class_uses_recursive($class));
112
113 90
        return isset($traits[$trait]);
114
    }
115
}
116
if (!function_exists('array_keys_exists')) {
117
    function array_keys_exists(array $keys, array $arr)
118
    {
119 56
        return !array_diff_key(array_flip($keys), $arr);
120
    }
121
}
122
123
if (!function_exists('array_is_subset_of')) {
124
    function array_is_subset_of(array $subset, array $array, bool $strict = false)
125
    {
126 56
        $arrayAssociative = is_associative_array($array);
127 56
        $subsetAssociative = is_associative_array($subset);
128
129 56
        if ($subsetAssociative && $arrayAssociative) {
130 56
            $patched = \array_replace_recursive($array, $subset);
131
132 56
            if ($strict) {
133
                $result = $array === $patched;
134
            } else {
135 56
                $result = $array == $patched;
136
            }
137
138 56
            return $result;
139 1
        } elseif (($subsetAssociative && !$arrayAssociative) ||
140 1
            (!$subsetAssociative && $arrayAssociative)) {
141
            return false;
142
        }
143
144 1
        $result = array_intersect($subset, $array);
145
146 1
        if ($strict) {
147
            return $result === $subset;
148
        }
149
150 1
        return $result == $subset;
151
    }
152
}
153
154
if (!function_exists('is_associative_array')) {
155
    function is_associative_array(array $arr)
156
    {
157 57
        if ([] === $arr) {
158
            return false;
159
        }
160
161 57
        return array_keys($arr) !== range(0, count($arr) - 1);
162
    }
163
}
164
165
if (!function_exists('get_class_property')) {
166
    function get_class_property($class, string $property)
167
    {
168 90
        if (!is_string($class)) {
169
            $class = get_class($class);
170
        }
171
172
        try {
173 90
            $reflectionClass = new \ReflectionClass($class);
174 90
            $property = $reflectionClass->getProperty($property);
175 90
            $property->setAccessible(true);
176 90
        } catch (ReflectionException $e) {
177 90
            return;
178
        }
179
180 90
        return $property->getValue($reflectionClass->newInstanceWithoutConstructor());
181
    }
182
}
183
184
if (!function_exists('instance_without_constructor')) {
185
    function instance_without_constructor($class)
186
    {
187 90
        if (!is_string($class)) {
188
            $class = get_class($class);
189
        }
190
191
        try {
192 90
            $reflectionClass = new \ReflectionClass($class);
193
        } catch (ReflectionException $e) {
194
            return null;
195
        }
196 90
        if ($reflectionClass->isAbstract()) {
197 90
            return null;
198
        }
199
200 90
        return $reflectionClass->newInstanceWithoutConstructor();
201
    }
202
}
203
204
if (!function_exists('call_class_function')) {
205
    function call_class_function($class, string $methodName)
206
    {
207 90
        return instance_without_constructor($class)->$methodName();
208
    }
209
}
210
211
if (!function_exists('get_class_constants')) {
212
    function get_class_constants($class)
213
    {
214 90
        if (!is_string($class)) {
215
            $class = get_class($class);
216
        }
217
218
        try {
219 90
            $reflectionClass = new \ReflectionClass($class);
220
        } catch (ReflectionException $e) {
221
            return;
222
        }
223
224 90
        return $reflectionClass->getConstants();
225
    }
226
}
227