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

AccessToken::setClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 4
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace OAuth;
4
5
use DateTime;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
8
use League\OAuth2\Server\Entities\ClientEntityInterface;
9
use League\OAuth2\Server\Entities\ScopeEntityInterface;
10
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait;
11
12
/**
13
* @Entity(repositoryClass="OAuth\Repository\AccessTokenRepository")
14
* @Table(name="AccessToken")
15
*/
16 View Code Duplication
class AccessToken implements AccessTokenEntityInterface
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
    use AccessTokenTrait;
19
20
    /**
21
     * @var ArrayCollection $scopes
22
     * @ManyToMany(targetEntity="OAuth\Scope", inversedBy="accessTokens")
23
     * @JoinTable(name="AccessTokenScope",
24
     *      joinColumns={@JoinColumn(name="scopeId", referencedColumnName="identifier")},
25
     *      inverseJoinColumns={@JoinColumn(name="accessTokenId", referencedColumnName="identifier")}
26
     *      )
27
     */
28
    protected $scopes;
29
30
    /**
31
     * @var DateTime
32
     * @Column(type="date",nullable=true)
33
     */
34
    protected $expiryDateTime;
35
36
    /**
37
     * @var User
38
     * @Column(type="integer", length=11)
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 6
    public function __construct()
57
    {
58 6
        $this->scopes = new ArrayCollection();
59 6
    }
60
61
    /**
62
     * Set token
63
     *
64
     * @param string $token
65
     * @return AccessToken
66
     */
67
    public function setToken($token)
68
    {
69
        $this->token = $token;
0 ignored issues
show
Bug introduced by
The property token does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
70
        return $this;
71
    }
72
73
    /**
74
     * Get token
75
     *
76
     * @return string
77
     */
78 2
    public function getIdentifier()
79
    {
80 2
        return $this->identifier;
81
    }
82
83
    /**
84
     * @param string $identifier
85
     */
86 2
    public function setIdentifier($identifier)
87
    {
88 2
        $this->identifier = $identifier;
89 2
    }
90
91
    /**
92
     * @param ScopeEntityInterface $scope
93
     * @return $this
94
     */
95 1
    public function addScope(ScopeEntityInterface $scope)
96
    {
97 1
        $this->scopes->add($scope);
98 1
        return $this;
99
    }
100
101
    /**
102
     * Return an array of scopes associated with the token.
103
     *
104
     * @return ScopeEntityInterface[]
105
     */
106 1
    public function getScopes()
107
    {
108 1
        return $this->scopes->toArray();
109
    }
110
111
    /**
112
     * Get the token's expiry date time.
113
     *
114
     * @return DateTime
115
     */
116 1
    public function getExpiryDateTime()
117
    {
118 1
        return $this->expiryDateTime;
119
    }
120
121
    /**
122
     * Set the date time when the token expires.
123
     *
124
     * @param DateTime $dateTime
125
     */
126 1
    public function setExpiryDateTime(DateTime $dateTime)
127
    {
128 1
        $this->expiryDateTime = $dateTime;
129 1
    }
130
131
    /**
132
     * @param int $identifier
133
     * @return $this
134
     */
135 1
    public function setUserIdentifier($identifier)
136
    {
137 1
        $this->userIdentifier = $identifier;
0 ignored issues
show
Documentation Bug introduced by
It seems like $identifier of type 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...
138 1
        return $this;
139
    }
140
141
    /**
142
     * Get the token user's identifier.
143
     *
144
     * @return int
145
     */
146 1
    public function getUserIdentifier()
147
    {
148 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...
149
    }
150
151
    /**
152
     * Get the client that the token was issued to.
153
     *
154
     * @return ClientEntityInterface
155
     */
156 1
    public function getClient()
157
    {
158 1
        return $this->client;
159
    }
160
161
    /**
162
     * Set the client that the token was issued to.
163
     *
164
     * @param ClientEntityInterface $client
165
     */
166 1
    public function setClient(ClientEntityInterface $client)
167
    {
168 1
        $this->client = $client;
169
    }
170
}