Issues (3627)

bundles/ApiBundle/Entity/oAuth1/AccessToken.php (2 issues)

loose comparison of integers.

Best Practice Bug Major
1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\ApiBundle\Entity\oAuth1;
13
14
use Bazinga\OAuthServerBundle\Model\AccessTokenInterface;
15
use Bazinga\OAuthServerBundle\Model\ConsumerInterface;
16
use Doctrine\ORM\Mapping as ORM;
17
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
18
use Symfony\Component\Security\Core\User\UserInterface;
19
20
/**
21
 * Class AccessToken.
22
 */
23
class AccessToken implements AccessTokenInterface
24
{
25
    /**
26
     * @var int
27
     */
28
    protected $id;
29
30
    /**
31
     * @var Consumer
32
     */
33
    protected $consumer;
34
35
    /**
36
     * @var \Mautic\UserBundle\Entity\User
37
     */
38
    protected $user;
39
40
    /**
41
     * @var string
42
     */
43
    protected $token;
44
45
    /**
46
     * @var string
47
     */
48
    protected $secret;
49
50
    /**
51
     * @var int|null
52
     */
53
    protected $expiresAt;
54
55
    public static function loadMetadata(ORM\ClassMetadata $metadata)
56
    {
57
        $builder = new ClassMetadataBuilder($metadata);
58
59
        $builder->setTable('oauth1_access_tokens')
60
            ->addIndex(['token'], 'oauth1_access_token_search');
61
62
        $builder->createField('id', 'integer')
63
            ->isPrimaryKey()
64
            ->generatedValue()
65
            ->build();
66
67
        $builder->createManyToOne('consumer', 'Consumer')
68
            ->inversedBy('accessTokens')
69
            ->addJoinColumn('consumer_id', 'id', false, false, 'CASCADE')
70
            ->build();
71
72
        $builder->createManyToOne('user', 'Mautic\UserBundle\Entity\User')
73
            ->addJoinColumn('user_id', 'id', false, false, 'CASCADE')
74
            ->build();
75
76
        $builder->addField('token', 'string');
77
78
        $builder->addField('secret', 'string');
79
80
        $builder->createField('expiresAt', 'bigint')
81
            ->columnName('expires_at')
82
            ->nullable()
83
            ->build();
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getId()
90
    {
91
        return $this->id;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getToken()
98
    {
99
        return $this->token;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function setToken($token)
106
    {
107
        $this->token = $token;
108
109
        return $this;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function getSecret()
116
    {
117
        return $this->secret;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function setSecret($secret)
124
    {
125
        $this->secret = $secret;
126
127
        return $this;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getExpiresAt()
134
    {
135
        return $this->expiresAt;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function setExpiresAt($expiresAt)
142
    {
143
        $this->expiresAt = $expiresAt;
144
145
        return $this;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function getExpiresIn()
152
    {
153
        if ($this->expiresAt) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->expiresAt of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
154
            return $this->expiresAt - time();
155
        }
156
157
        return PHP_INT_MAX;
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function hasExpired()
164
    {
165
        if ($this->expiresAt) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->expiresAt of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
166
            return time() > $this->expiresAt;
167
        }
168
169
        return false;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function getUser()
176
    {
177
        return $this->user;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function setUser(UserInterface $user)
184
    {
185
        $this->user = $user;
186
187
        return $this;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function setConsumer(ConsumerInterface $consumer)
194
    {
195
        $this->consumer = $consumer;
196
197
        return $this;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    public function getConsumer()
204
    {
205
        return $this->consumer;
206
    }
207
}
208