Completed
Push — master ( a9822c...8aa0e0 )
by Derek Stephen
14:27
created

AuthCode::getUserIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace OAuth;
4
5
use DateTime;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\ORM\Mapping as ORM;
8
use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
9
use League\OAuth2\Server\Entities\ClientEntityInterface;
10
use League\OAuth2\Server\Entities\ScopeEntityInterface;
11
12
/**
13
 * @ORM\Entity(repositoryClass="OAuth\Repository\AuthCodeRepository")
14
 * @ORM\Table(name="AuthCode")
15
 */
16 View Code Duplication
class AuthCode implements AuthCodeEntityInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
19
    /**
20
     * @var null|string
21
     * @ORM\Column(type="string", length=255, nullable=true)
22
     */
23
    protected $redirectUri;
24
25
    /**
26
     * @var ArrayCollection $scopes
27
     */
28
    protected $scopes;
29
30
    /**
31
     * @var DateTime
32
     * @ORM\Column(type="datetime",nullable=true)
33
     */
34
    protected $expiryDateTime;
35
36
    /**
37
     * @var OAuthUser
38
     * @ORM\OneToOne(targetEntity="OAuth\OAuthUser")
39
     * @ORM\JoinColumn(name="client", referencedColumnName="id")
40
     */
41
    protected $userIdentifier;
42
43
    /**
44
     * @var ClientEntityInterface
45
     * @ORM\ManyToOne(targetEntity="OAuth\Client")
46
     * @ORM\JoinColumn(name="client", referencedColumnName="id")
47
     */
48
    protected $client;
49
50
    /**
51
     * @var string
52
     * @ORM\Column(type="text", nullable=false)
53
     */
54
    protected $identifier;
55
56
    /**
57
     * @var int
58
     * @ORM\Id
59
     * @ORM\Column(type="integer", nullable=false)
60
     * @ORM\GeneratedValue
61
     */
62
    protected $id;
63
64 5
    public function __construct()
65
    {
66 5
        $this->scopes = new ArrayCollection();
67 5
    }
68
69
    /**
70
     * @return string
71
     */
72 1
    public function getIdentifier()
73
    {
74 1
        return $this->identifier;
75
    }
76
77
    /**
78
     * @param string $identifier
79
     */
80 1
    public function setIdentifier($identifier)
81
    {
82 1
        $this->identifier = $identifier;
83 1
    }
84
85
    /**
86
     * @param ScopeEntityInterface $scope
87
     * @return $this
88
     */
89 1
    public function addScope(ScopeEntityInterface $scope)
90
    {
91 1
        $this->scopes->add($scope);
92 1
        return $this;
93
    }
94
95
    /**
96
     * Return an array of scopes associated with the token.
97
     *
98
     * @return ScopeEntityInterface[]
99
     */
100 1
    public function getScopes()
101
    {
102 1
        return $this->scopes->toArray();
103
    }
104
105
    /**
106
     * Get the token's expiry date time.
107
     *
108
     * @return DateTime
109
     */
110 1
    public function getExpiryDateTime()
111
    {
112 1
        return $this->expiryDateTime;
113
    }
114
115
    /**
116
     * Set the date time when the token expires.
117
     *
118
     * @param DateTime $dateTime
119
     */
120 1
    public function setExpiryDateTime(DateTime $dateTime)
121
    {
122 1
        $this->expiryDateTime = $dateTime;
123 1
    }
124
125
    /**
126
     * Set the identifier of the user associated with the token.
127
     *
128
     * @param User $identifier The identifier of the user
129
     */
130
    public function setUserIdentifier($identifier)
131
    {
132
        $this->userIdentifier = $identifier;
0 ignored issues
show
Documentation Bug introduced by
It seems like $identifier of type object<OAuth\User> is incompatible with the declared type object<OAuth\OAuthUser> 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...
133
    }
134
135
    /**
136
     * Get the token user's identifier.
137
     *
138
     * @return User
139
     */
140
    public function getUserIdentifier()
141
    {
142
        return $this->userIdentifier;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->userIdentifier; (OAuth\OAuthUser) 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...
143
    }
144
145
    /**
146
     * Get the client that the token was issued to.
147
     *
148
     * @return ClientEntityInterface
149
     */
150 1
    public function getClient()
151
    {
152 1
        return $this->client;
153
    }
154
155
    /**
156
     * Set the client that the token was issued to.
157
     *
158
     * @param ClientEntityInterface $client
159
     */
160 1
    public function setClient(ClientEntityInterface $client)
161
    {
162 1
        $this->client = $client;
163 1
    }
164
165
    /**
166
     * @return string
167
     */
168 1
    public function getRedirectUri()
169
    {
170 1
        return $this->redirectUri;
171
    }
172
173
    /**
174
     * @param string $uri
175
     */
176 1
    public function setRedirectUri($uri)
177
    {
178 1
        $this->redirectUri = $uri;
179
    }
180
}