Passed
Push — master ( 26d803...3c9253 )
by Marcos
01:51
created

RefreshToken   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 2
cbo 0
dl 0
loc 141
ccs 20
cts 24
cp 0.8333
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A getRefreshToken() 0 4 1
A setValid() 0 6 1
A getValid() 0 4 1
A setUsername() 0 6 1
A getUsername() 0 4 1
A isValid() 0 6 2
A setRefreshToken() 0 10 2
A __toString() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the GesdinetJWTRefreshTokenBundle package.
5
 *
6
 * (c) Gesdinet <http://www.gesdinet.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gesdinet\JWTRefreshTokenBundle\Entity;
13
14
use Doctrine\ORM\Mapping as ORM;
15
use Symfony\Component\Validator\Constraints as Assert;
16
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenInterface;
17
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
18
19
/**
20
 * Refresh Token.
21
 *
22
 * @ORM\Table("refresh_tokens")
23
 * @ORM\Entity(repositoryClass="Gesdinet\JWTRefreshTokenBundle\Entity\RefreshTokenRepository")
24
 * @UniqueEntity("refreshToken")
25
 */
26
class RefreshToken implements RefreshTokenInterface
27
{
28
    /**
29
     * @var int
30
     *
31
     * @ORM\Column(name="id", type="integer")
32
     * @ORM\Id
33
     * @ORM\GeneratedValue(strategy="AUTO")
34
     */
35
    protected $id;
36
37
    /**
38
     * @var string
39
     *
40
     * @ORM\Column(name="refresh_token", type="string", length=128, unique=true)
41
     * @Assert\NotBlank()
42
     */
43
    protected $refreshToken;
44
45
    /**
46
     * @var string
47
     *
48
     * @ORM\Column(name="username", type="string", length=255)
49
     * @Assert\NotBlank()
50
     */
51
    protected $username;
52
53
    /**
54
     * @var string
55
     *
56
     * @ORM\Column(name="valid", type="datetime")
57
     * @Assert\NotBlank()
58
     */
59
    protected $valid;
60
61
    /**
62
     * Get id.
63
     *
64
     * @return int
65
     */
66
    public function getId()
67
    {
68
        return $this->id;
69
    }
70
71
    /**
72
     * Set refreshToken.
73
     *
74
     * @param string $refreshToken
75
     *
76
     * @return RefreshToken
77
     */
78 6
    public function setRefreshToken($refreshToken = null)
79
    {
80 6
        if (null == $refreshToken) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $refreshToken of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
81 6
            $this->refreshToken = bin2hex(openssl_random_pseudo_bytes(64));
82
        } else {
83 1
            $this->refreshToken = $refreshToken;
84
        }
85
86 6
        return $this;
87
    }
88
89
    /**
90
     * Get refreshToken.
91
     *
92
     * @return string
93
     */
94 1
    public function getRefreshToken()
95
    {
96 1
        return $this->refreshToken;
97
    }
98
99
    /**
100
     * Set valid.
101
     *
102
     * @param \DateTime $valid
103
     *
104
     * @return RefreshToken
105
     */
106 3
    public function setValid($valid)
107
    {
108 3
        $this->valid = $valid;
0 ignored issues
show
Documentation Bug introduced by
It seems like $valid of type object<DateTime> is incompatible with the declared type string of property $valid.

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...
109
110 3
        return $this;
111
    }
112
113
    /**
114
     * Get valid.
115
     *
116
     * @return \DateTime
117
     */
118 1
    public function getValid()
119
    {
120 1
        return $this->valid;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->valid; (string) is incompatible with the return type declared by the interface Gesdinet\JWTRefreshToken...okenInterface::getValid of type DateTime.

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...
121
    }
122
123
    /**
124
     * Set username.
125
     *
126
     * @param string $username
127
     *
128
     * @return RefreshToken
129
     */
130 6
    public function setUsername($username)
131
    {
132 6
        $this->username = $username;
133
134 6
        return $this;
135
    }
136
137
    /**
138
     * Get username.
139
     *
140
     * @return string
141
     */
142 1
    public function getUsername()
143
    {
144 1
        return $this->username;
145
    }
146
147
    /**
148
     * Check if is a valid refresh token.
149
     *
150
     * @return bool
151
     */
152 2
    public function isValid()
153
    {
154 2
        $datetime = new \DateTime();
155
156 2
        return ($this->valid >= $datetime) ? true : false;
157
    }
158
159
    /**
160
     * @return string Refresh Token
161
     */
162
    public function __toString()
163
    {
164
        return $this->getRefreshToken();
165
    }
166
}
167