Failed Conditions
Push — master ( 1312dd...bb342a )
by Florent
04:40
created

User::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\ServerBundle\Tests\TestBundle\Entity;
15
16
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
17
use OAuth2Framework\Component\Core\UserAccount\UserAccount;
18
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
19
use Symfony\Component\Security\Core\User\EquatableInterface;
20
use Symfony\Component\Security\Core\User\UserInterface;
21
22
class User implements UserInterface, UserAccount, EquatableInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private $username;
28
29
    /**
30
     * @var string
31
     */
32
    private $password;
33
34
    /**
35
     * @var null|string
36
     */
37
    private $salt;
38
39
    /**
40
     * @var string[]
41
     */
42
    private $roles;
43
44
    /**
45
     * @var string[]
46
     */
47
    private $oauth2Passwords = [];
48
49
    /**
50
     * @var UserAccountId
51
     */
52
    private $publicId;
53
54
    /**
55
     * @var \DateTimeImmutable|null
56
     */
57
    private $lastLoginAt;
58
59
    /**
60
     * @var \DateTimeImmutable|null
61
     */
62
    private $lastUpdateAt;
63
64
    /**
65
     * @var array
66
     */
67
    private $parameters = [];
68
69
    /**
70
     * @param string                  $username
71
     * @param string                  $password
72
     * @param string|null             $salt
73
     * @param string[]                $roles
74
     * @param string[]                $oauth2Passwords
75
     * @param UserAccountId           $publicId
76
     * @param \DateTimeImmutable|null $lastLoginAt
77
     * @param \DateTimeImmutable|null $lastUpdateAt
78
     * @param array                   $parameters
79
     */
80
    public function __construct(string $username, string $password, string $salt = null, array $roles, array $oauth2Passwords, UserAccountId $publicId, ?\DateTimeImmutable $lastLoginAt = null, ?\DateTimeImmutable $lastUpdateAt = null, array $parameters = [])
81
    {
82
        $this->username = $username;
83
        $this->password = $password;
84
        $this->salt = $salt;
85
        $this->roles = $roles;
86
        $this->oauth2Passwords = $oauth2Passwords;
87
        $this->publicId = $publicId;
88
        $this->lastLoginAt = $lastLoginAt;
89
        $this->lastUpdateAt = $lastUpdateAt;
90
        $this->parameters = $parameters;
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function getOAuth2Passwords(): array
97
    {
98
        return $this->oauth2Passwords;
99
    }
100
101
    /**
102
     * @return ResourceOwnerId
103
     */
104
    public function getPublicId(): ResourceOwnerId
105
    {
106
        return $this->publicId;
107
    }
108
109
    public function getUserAccountId(): UserAccountId
110
    {
111
        $id = $this->getPublicId();
112
        if (!$id instanceof UserAccountId) {
113
            throw new \RuntimeException();
114
        }
115
116
        return $id;
117
    }
118
119
    /**
120
     * @return int|null
121
     */
122
    public function getLastLoginAt(): ?int
123
    {
124
        return $this->lastLoginAt ? $this->lastLoginAt->getTimestamp() : null;
125
    }
126
127
    /**
128
     * @return int|null
129
     */
130
    public function getLastUpdateAt(): ?int
131
    {
132
        return $this->lastUpdateAt ? $this->lastUpdateAt->getTimestamp() : null;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function has(string $key): bool
139
    {
140
        return array_key_exists($key, $this->parameters);
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function get(string $key)
147
    {
148
        if (!$this->has($key)) {
149
            throw new \InvalidArgumentException(sprintf('Configuration value with key "%s" does not exist.', $key));
150
        }
151
152
        return $this->parameters[$key];
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function getRoles(): array
159
    {
160
        return $this->roles;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function getSalt()
167
    {
168
        return $this->salt;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function getUsername(): string
175
    {
176
        return $this->username;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function getPassword(): string
183
    {
184
        return $this->password;
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function eraseCredentials()
191
    {
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function isEqualTo(UserInterface $user)
198
    {
199
        if (!$user instanceof self) {
200
            return false;
201
        }
202
203
        if ($this->password !== $user->getPassword()) {
204
            return false;
205
        }
206
207
        if ($this->getSalt() !== $user->getSalt()) {
208
            return false;
209
        }
210
211
        if ($this->username !== $user->getUsername()) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($this->username... $user->getUsername());.
Loading history...
212
            return false;
213
        }
214
215
        return true;
216
    }
217
}
218