Passed
Pull Request — master (#71)
by
unknown
02:32
created

AbstractRefreshToken::setRefreshToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 6
nop 1
crap 2
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
 * Abstract Refresh Token.
21
 *
22
 * @ORM\MappedSuperclass()
23
 * @UniqueEntity("refreshToken")
24
 */
25
abstract class AbstractRefreshToken implements RefreshTokenInterface
26
{
27
28
    /**
29
     * @var string
30
     *
31
     * @ORM\Column(name="refresh_token", type="string", length=128, unique=true)
32
     * @Assert\NotBlank()
33
     */
34
    private $refreshToken;
35
36
    /**
37
     * @var string
38
     *
39
     * @ORM\Column(name="username", type="string", length=255)
40
     * @Assert\NotBlank()
41
     */
42
    private $username;
43
44
    /**
45
     * @var string
46
     *
47
     * @ORM\Column(name="valid", type="datetime")
48
     * @Assert\NotBlank()
49
     */
50
    private $valid;
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    abstract public function getId();
56
57
    /**
58
     * Set refreshToken.
59
     *
60
     * @param string $refreshToken
61
     *
62
     * @return RefreshToken
63
     */
64 6
    public function setRefreshToken($refreshToken = null)
65
    {
66 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...
67 6
            $this->refreshToken = bin2hex(openssl_random_pseudo_bytes(64));
68
        } else {
69 1
            $this->refreshToken = $refreshToken;
70
        }
71
72 6
        return $this;
73
    }
74
75
    /**
76
     * Get refreshToken.
77
     *
78
     * @return string
79
     */
80 1
    public function getRefreshToken()
81
    {
82 1
        return $this->refreshToken;
83
    }
84
85
    /**
86
     * Set valid.
87
     *
88
     * @param \DateTime $valid
89
     *
90
     * @return RefreshToken
91
     */
92 3
    public function setValid($valid)
93
    {
94 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...
95
96 3
        return $this;
97
    }
98
99
    /**
100
     * Get valid.
101
     *
102
     * @return \DateTime
103
     */
104 1
    public function getValid()
105
    {
106 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...
107
    }
108
109
    /**
110
     * Set username.
111
     *
112
     * @param string $username
113
     *
114
     * @return RefreshToken
115
     */
116 6
    public function setUsername($username)
117
    {
118 6
        $this->username = $username;
119
120 6
        return $this;
121
    }
122
123
    /**
124
     * Get username.
125
     *
126
     * @return string
127
     */
128 1
    public function getUsername()
129
    {
130 1
        return $this->username;
131
    }
132
133
    /**
134
     * Check if is a valid refresh token.
135
     *
136
     * @return bool
137
     */
138 2
    public function isValid()
139
    {
140 2
        $datetime = new \DateTime();
141
142 2
        return ($this->valid >= $datetime) ? true : false;
143
    }
144
145
    /**
146
     * @return string Refresh Token
147
     */
148
    public function __toString()
149
    {
150
        return $this->getRefreshToken();
151
    }
152
}
153