blameable_user()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 6
eloc 7
c 6
b 0
f 0
nc 10
nop 1
dl 0
loc 14
rs 9.2222
1
<?php
2
3
use Illuminate\Database\Eloquent\Model;
4
use RichanFongdasen\EloquentBlameable\BlameableService;
5
6
if (!function_exists('blameable_user')) {
7
8
    /**
9
     * Get the blameable User identifier.
10
     *
11
     * @param \Illuminate\Database\Eloquent\Model $model
12
     *
13
     * @return int|string|null
14
     */
15
    function blameable_user(Model $model)
16
    {
17
        $guard = app(BlameableService::class)->getConfiguration($model, 'guard');
18
19
        $user = ($guard === null) ? app('auth')->user() : app('auth')->guard($guard)->user();
20
        $userClass = (string) app(BlameableService::class)->getConfiguration($model, 'user');
21
22
        if (($user instanceof Model) && ($user instanceof $userClass)) {
23
            $userId = $user->getKey();
24
25
            return (is_int($userId) || is_string($userId)) ? $userId : null;
26
        }
27
28
        return null;
29
    }
30
}
31