Completed
Push — master ( efbe60...e38209 )
by Derek Stephen
05:09
created

AuthCode::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace OAuth;
4
5
use DateTime;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
8
use League\OAuth2\Server\Entities\ClientEntityInterface;
9
use League\OAuth2\Server\Entities\ScopeEntityInterface;
10
11
class AuthCode implements AuthCodeEntityInterface
12
{
13
14
    /**
15
     * @var null|string
16
     * @Column(type="string", length=255, nullable=true)
17
     */
18
    protected $redirectUri;
19
20
    /**
21
     * @var ArrayCollection $scopes
22
     * @ManyToMany(targetEntity="OAuth\Scope")
23
     * @JoinTable(name="AuthTokenScope",
24
     *      joinColumns={@JoinColumn(name="scopeId", referencedColumnName="identifier")},
25
     *      inverseJoinColumns={@JoinColumn(name="authTokenId", referencedColumnName="identifier")}
26
     */
27
    protected $scopes;
28
29
    /**
30
     * @var DateTime
31
     * @Column(type="date",nullable=true)
32
     */
33
    protected $expiryDateTime;
34
35
    /**
36
     * @var User
37
     * @ManyToOne(targetEntity="OAuth\User")
38
     * @JoinColumn(name="client", referencedColumnName="id")
39
     */
40
    protected $userIdentifier;
41
42
    /**
43
     * @var ClientEntityInterface
44
     * @ManyToOne(targetEntity="OAuth\Client")
45
     * @JoinColumn(name="client", referencedColumnName="identifier")
46
     */
47
    protected $client;
48
49
    /**
50
     * @var string
51
     * @Id
52
     * @Column(type="string", length=40)
53
     */
54
    protected $identifier;
55
56
    public function __construct()
57
    {
58
        $this->scopes = new ArrayCollection();
59
    }
60
61
    /**
62
     * @return mixed
63
     */
64
    public function getIdentifier()
65
    {
66
        return $this->identifier;
67
    }
68
69
    /**
70
     * @param mixed $identifier
71
     */
72
    public function setIdentifier($identifier)
73
    {
74
        $this->identifier = $identifier;
75
    }
76
77
    /**
78
     * Associate a scope with the token.
79
     *
80
     * @param ScopeEntityInterface $scope
81
     */
82
    public function addScope(ScopeEntityInterface $scope)
83
    {
84
        $this->scopes[$scope->getIdentifier()] = $scope;
85
    }
86
87
    /**
88
     * Return an array of scopes associated with the token.
89
     *
90
     * @return ScopeEntityInterface[]
91
     */
92
    public function getScopes()
93
    {
94
        return array_values($this->scopes);
95
    }
96
97
    /**
98
     * Get the token's expiry date time.
99
     *
100
     * @return \DateTime
101
     */
102
    public function getExpiryDateTime()
103
    {
104
        return $this->expiryDateTime;
105
    }
106
107
    /**
108
     * Set the date time when the token expires.
109
     *
110
     * @param DateTime $dateTime
111
     */
112
    public function setExpiryDateTime(DateTime $dateTime)
113
    {
114
        $this->expiryDateTime = $dateTime;
115
    }
116
117
    /**
118
     * Set the identifier of the user associated with the token.
119
     *
120
     * @param string|int $identifier The identifier of the user
121
     */
122
    public function setUserIdentifier($identifier)
123
    {
124
        $this->userIdentifier = $identifier;
0 ignored issues
show
Documentation Bug introduced by
It seems like $identifier of type string or integer is incompatible with the declared type object<OAuth\User> of property $userIdentifier.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
125
    }
126
127
    /**
128
     * Get the token user's identifier.
129
     *
130
     * @return string|int
131
     */
132
    public function getUserIdentifier()
133
    {
134
        return $this->userIdentifier;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->userIdentifier; (OAuth\User) is incompatible with the return type declared by the interface League\OAuth2\Server\Ent...face::getUserIdentifier of type string|integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
135
    }
136
137
    /**
138
     * Get the client that the token was issued to.
139
     *
140
     * @return ClientEntityInterface
141
     */
142
    public function getClient()
143
    {
144
        return $this->client;
145
    }
146
147
    /**
148
     * Set the client that the token was issued to.
149
     *
150
     * @param ClientEntityInterface $client
151
     */
152
    public function setClient(ClientEntityInterface $client)
153
    {
154
        $this->client = $client;
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getRedirectUri()
161
    {
162
        return $this->redirectUri;
163
    }
164
165
    /**
166
     * @param string $uri
167
     */
168
    public function setRedirectUri($uri)
169
    {
170
        $this->redirectUri = $uri;
171
    }
172
}