Completed
Push — master ( 4ad177...ddee2e )
by Mathias
33s queued 17s
created

AnonymousUser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 17
dl 0
loc 106
ccs 19
cts 21
cp 0.9048
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setPassword() 0 3 1
A setCredential() 0 3 1
A preventPersistence() 0 3 1
A getRole() 0 3 1
A setSecret() 0 3 1
A getToken() 0 10 3
A getId() 0 6 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
6
 * @license   MIT
7
 */
8
9
namespace Auth\Entity;
10
11
use Core\Entity\AbstractIdentifiableEntity;
12
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
13
use Laminas\Session\Container as Session;
14
15
/**
16
 * An Anonymous user.
17
 *
18
 * Anonymous user may not be persisted in the database and is used solely
19
 * for identifying an user through subsequential requests by a session variable.
20
 *
21
 * @author Mathias Gelhausen <[email protected]>
22
 *
23
 * @ODM\Document(collection="users", repositoryClass="Auth\Repository\User")
24
 * @ODM\HasLifecycleCallbacks
25
 */
26
class AnonymousUser extends User
27
{
28
29
    /**
30
     * Unique identification key (session-based)
31
     * @var string
32
     */
33
    protected $token;
34
35
    public function __construct(?string $token = null)
36
    {
37
        $this->token = $token;
38
    }
39
40
    /**
41
     * Prevents this entity from persistence.
42
     *
43
     * This is a Doctrine Hook which throws an exception when called.
44 1
     *
45
     * @throws \RuntimeException
46 1
     * @ODM\PrePersist
47
     * @ODM\PreUpdate
48
     */
49
    public function preventPersistence()
50
    {
51
        throw new \RuntimeException('Anonymous users may not be persisted.');
52
    }
53
54 1
    /**
55
     * Gets the anonymous identification key.
56 1
     *
57 1
     * @return string
58 1
     */
59 1
    public function getToken()
60
    {
61 1
        if (!$this->token) {
62
            $session = new Session('Auth');
63 1
            if (!$session->token) {
64
                $session->token = uniqid();
65
            }
66
            $this->token = $session->token;
67
        }
68
        return $this->token;
69
    }
70
71
    /**
72
     * {@inheritDoc}
73 1
     *
74
     * Normalizes the token for use in Permission Entity
75 1
     *
76 1
     * @see \Core\Entity\AbstractIdentifiableEntity::getId()
77
     */
78 1
    public function getId()
79
    {
80
        if (!$this->id) {
81
            $this->setId('token:' . $this->getToken());
82
        }
83
        return $this->id;
84
    }
85
86
    /**
87
     * {@inheritDoc}
88 3
     *
89
     * Always returns "guest"
90 3
     *
91
     * @see \Auth\Entity\User::getRole()
92
     */
93
    public function getRole()
94
    {
95
        return "guest";
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     *
101
     * Disabled. Simply returns this instance.
102
     *
103
     * @see \Auth\Entity\User::setPassword()
104
     */
105
    public function setPassword($password)
106
    {
107
        return $this;
108
    }
109
110
    /**
111
     * {@inheritDoc}
112 1
     *
113
     * Disabled. Simply returns this instance.
114 1
     *
115
     * @see \Auth\Entity\User::setCredential()
116
     */
117
    public function setCredential($credential)
118
    {
119
        return $this;
120
    }
121
122
    /**
123
     * {@inheritDoc}
124 1
     *
125
     * Disabled. Simply returns this instance.
126 1
     *
127
     * @see \Auth\Entity\User::setSecret()
128
     */
129
    public function setSecret($secret)
130
    {
131
        return $this;
132
    }
133
}
134