Test Failed
Push — master ( 7ed30e...a835f3 )
by Julien
04:40
created

Identity::getCurrentUserId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 2
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 6
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Model;
13
14
use Zemit\Models\Interfaces\UserInterface;
15
16
trait Identity
17
{
18
    /**
19
     * Get the current identity service from the DI
20
     */
21
    public function getIdentity(): \Zemit\Identity
22
    {
23
        return $this->getDI()->get('identity');
0 ignored issues
show
Bug introduced by
It seems like getDI() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

23
        return $this->/** @scrutinizer ignore-call */ getDI()->get('identity');
Loading history...
24
    }
25
26
    /**
27
     * Return true or false whether a user is logged in or not into the current session
28
     */
29
    public function isLoggedIn(bool $as = false): bool
30
    {
31
        return $this->getIdentity()->isLoggedIn($as);
32
    }
33
34
    /**
35
     * Return true or false whether a user is logged in as or not into the current session
36
     */
37
    public function isLoggedInAs(): bool
38
    {
39
        return $this->isLoggedIn(true);
40
    }
41
42
    /**
43
     * Get the current active user using the identity service
44
     */
45
    public function getCurrentUser(bool $as = false): ?UserInterface
46
    {
47
        return $this->getIdentity()->getUser($as);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getIdentity()->getUser($as) could return the type boolean which is incompatible with the type-hinted return Zemit\Models\Interfaces\UserInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
48
    }
49
    
50
    /**
51
     * Get the current active user using the identity service
52
     */
53
    public function getCurrentUserAs(): ?UserInterface
54
    {
55
        return $this->getCurrentUser(true);
56
    }
57
58
    /**
59
     * Get the current user id
60
     */
61
    public function getCurrentUserId(bool $as = false): ?int
62
    {
63
        $user = $this->getCurrentUser($as);
64
        return $user? $user->getId() : null;
0 ignored issues
show
introduced by
$user is of type Zemit\Models\User, thus it always evaluated to true.
Loading history...
65
    }
66
    
67
    /**
68
     * Get current user id callable (used for events behaviors)
69
     */
70
    public function getCurrentUserIdCallback($as = false): \Closure
71
    {
72
        return function () use ($as) {
73
            return $this->getCurrentUserId($as);
74
        };
75
    }
76
}
77