Completed
Pull Request — master (#11)
by Arnold
03:10
created

StateTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 1
b 0
f 0
dl 0
loc 54
ccs 8
cts 8
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A user() 0 3 1
A context() 0 3 1
A forUser() 0 3 1
A inContextOf() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny\Auth\Authz;
6
7
use Jasny\Auth\AuthzInterface as Authz;
8
use Jasny\Auth\ContextInterface as Context;
9
use Jasny\Auth\UserInterface as User;
10
use Jasny\Immutable;
11
12
/**
13
 * Trait for keeping state (user and context) in authz service.
14
 */
15
trait StateTrait
16
{
17
    use Immutable\With;
18
19
    /**
20
     * Current authenticated user
21
     */
22
    protected ?User $user = null;
23
24
    /**
25
     * The authorization context. This could be an organization, where a user has specific roles per organization
26
     * rather than roles globally.
27
     */
28
    protected ?Context $context = null;
29
30
    /**
31
     * Get a copy of the service for the given user.
32
     *
33
     * @param User|null $user
34
     * @return static&Authz
35
     */
36 25
    public function forUser(?User $user): Authz
37
    {
38 25
        return $this->withProperty('user', $user);
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->withProperty('user', $user) returns the type Jasny\Auth\Authz\StateTrait which is incompatible with the type-hinted return Jasny\Auth\AuthzInterface.
Loading history...
39
    }
40
41
    /**
42
     * Get a copy of the service for the given context.
43
     * Returns $this if authz hasn't changed.
44
     *
45
     * @param Context|null $context
46
     * @return static&Authz
47
     */
48 1
    public function inContextOf(?Context $context): Authz
49
    {
50 1
        return $this->withProperty('context', $context);
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->withProperty('context', $context) returns the type Jasny\Auth\Authz\StateTrait which is incompatible with the type-hinted return Jasny\Auth\AuthzInterface.
Loading history...
51
    }
52
53
    /**
54
     * Get current authenticated user.
55
     *
56
     * @return User|null
57
     */
58 1
    final public function user(): ?User
59
    {
60 1
        return $this->user;
61
    }
62
63
    /**
64
     * Get the current context.
65
     */
66 1
    final public function context(): ?Context
67
    {
68 1
        return $this->context;
69
    }
70
}
71