Completed
Push — master ( 24ab37...36f73c )
by Derek Stephen
01:35
created

AuthCode::getClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 4
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
/**
12
 * @Entity(repositoryClass="OAuth\Repository\AuthCodeRepository")
13
 * @Table(name="AuthCode")
14
 */
15 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...
16
{
17
18
    /**
19
     * @var null|string
20
     * @Column(type="string", length=255, nullable=true)
21
     */
22
    protected $redirectUri;
23
24
    /**
25
     * @var ArrayCollection $scopes
26
     * @ManyToMany(targetEntity="OAuth\Scope")
27
     * @JoinTable(name="AuthTokenScope",
28
     *      joinColumns={@JoinColumn(name="scopeId", referencedColumnName="identifier")},
29
     *      inverseJoinColumns={@JoinColumn(name="authTokenId", referencedColumnName="identifier")})
30
     */
31
    protected $scopes;
32
33
    /**
34
     * @var DateTime
35
     * @Column(type="date",nullable=true)
36
     */
37
    protected $expiryDateTime;
38
39
    /**
40
     * @var User
41
     * @OneToOne(targetEntity="OAuth\User")
42
     * @JoinColumn(name="client", referencedColumnName="id")
43
     */
44
    protected $userIdentifier;
45
46
    /**
47
     * @var ClientEntityInterface
48
     * @ManyToOne(targetEntity="OAuth\Client")
49
     * @JoinColumn(name="client", referencedColumnName="identifier")
50
     */
51
    protected $client;
52
53
    /**
54
     * @var string
55
     * @Id
56
     * @Column(type="string", length=40)
57
     */
58
    protected $identifier;
59
60 6
    public function __construct()
61
    {
62 6
        $this->scopes = new ArrayCollection();
63 6
    }
64
65
    /**
66
     * @return string
67
     */
68 1
    public function getIdentifier()
69
    {
70 1
        return $this->identifier;
71
    }
72
73
    /**
74
     * @param string $identifier
75
     */
76 1
    public function setIdentifier($identifier)
77
    {
78 1
        $this->identifier = $identifier;
79 1
    }
80
81
    /**
82
     * @param ScopeEntityInterface $scope
83
     * @return $this
84
     */
85 1
    public function addScope(ScopeEntityInterface $scope)
86
    {
87 1
        $this->scopes->add($scope);
88 1
        return $this;
89
    }
90
91
    /**
92
     * Return an array of scopes associated with the token.
93
     *
94
     * @return ScopeEntityInterface[]
95
     */
96 1
    public function getScopes()
97
    {
98 1
        return $this->scopes->toArray();
99
    }
100
101
    /**
102
     * Get the token's expiry date time.
103
     *
104
     * @return DateTime
105
     */
106 1
    public function getExpiryDateTime()
107
    {
108 1
        return $this->expiryDateTime;
109
    }
110
111
    /**
112
     * Set the date time when the token expires.
113
     *
114
     * @param DateTime $dateTime
115
     */
116 1
    public function setExpiryDateTime(DateTime $dateTime)
117
    {
118 1
        $this->expiryDateTime = $dateTime;
119 1
    }
120
121
    /**
122
     * Set the identifier of the user associated with the token.
123
     *
124
     * @param User $identifier The identifier of the user
125
     */
126 1
    public function setUserIdentifier($identifier)
127
    {
128 1
        $this->userIdentifier = $identifier;
129 1
    }
130
131
    /**
132
     * Get the token user's identifier.
133
     *
134
     * @return User
135
     */
136 1
    public function getUserIdentifier()
137
    {
138 1
        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...
139
    }
140
141
    /**
142
     * Get the client that the token was issued to.
143
     *
144
     * @return ClientEntityInterface
145
     */
146 1
    public function getClient()
147
    {
148 1
        return $this->client;
149
    }
150
151
    /**
152
     * Set the client that the token was issued to.
153
     *
154
     * @param ClientEntityInterface $client
155
     */
156 1
    public function setClient(ClientEntityInterface $client)
157
    {
158 1
        $this->client = $client;
159 1
    }
160
161
    /**
162
     * @return string
163
     */
164 1
    public function getRedirectUri()
165
    {
166 1
        return $this->redirectUri;
167
    }
168
169
    /**
170
     * @param string $uri
171
     */
172 1
    public function setRedirectUri($uri)
173
    {
174 1
        $this->redirectUri = $uri;
175
    }
176
}