Passed
Push — master ( 34e91e...8ddd25 )
by Luiz Kim
02:42
created

User::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ControleOnline\Entity;
4
5
use ApiPlatform\Metadata\GetCollection;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Metadata\GetCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use ApiPlatform\Metadata\Get;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Metadata\Get was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use ApiPlatform\Metadata\ApiResource;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Metadata\ApiResource was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use ApiPlatform\Metadata\ApiProperty;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Metadata\ApiProperty was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Doctrine\Orm\Filter\SearchFilter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use ApiPlatform\Metadata\ApiFilter;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Metadata\ApiFilter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use ApiPlatform\Metadata\Post;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Metadata\Post was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use ApiPlatform\Metadata\Put;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Metadata\Put was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use ApiPlatform\Metadata\Delete;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Metadata\Delete was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use ControleOnline\Controller\ChangeApiKeyAction;
15
use ControleOnline\Controller\ChangePasswordAction;
16
use ControleOnline\Controller\CreateUserAction;
17
use ControleOnline\Controller\Oauth\GoogleConnectController;
18
use ControleOnline\Controller\Oauth\GoogleReturnController;
19
use ControleOnline\Controller\Oauth\MercadolivreReturnController;
20
use ControleOnline\Controller\SecurityController;
21
use ControleOnline\Entity\People;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\People was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Symfony\Component\Security\Core\User\UserInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Security\Core\User\UserInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Doctrine\ORM\Mapping as ORM;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Mapping was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Doctrine\Common\Collections\ArrayCollection;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Collections\ArrayCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Doctrine\Common\Collections\Collection;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Collections\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Symfony\Component\Serializer\Annotation\Groups;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Serializer\Annotation\Groups was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
/**
29
 * User
30
 *
31
 * @ORM\EntityListeners ({ControleOnline\Listener\LogListener::class})  
32
 * @ORM\Entity (repositoryClass="ControleOnline\Repository\UserRepository")
33
 * @ORM\Table (name="users", uniqueConstraints={@ORM\UniqueConstraint (name="user_name", columns={"username"}), @ORM\UniqueConstraint(name="api_key", columns={"api_key"})}, indexes={@ORM\Index (name="people_id", columns={"people_id"})})
34
 * @ORM\HasLifecycleCallbacks
35
 */
36
37
38
39
#[ApiResource(
40
    operations: [
41
        new GetCollection(
42
            security: 'is_granted(\'ROLE_CLIENT\')',
43
            uriTemplate: '/oauth/google/connect',
44
            controller: GoogleConnectController::class,
45
        ),
46
        new GetCollection(
47
            security: 'is_granted(\'ROLE_CLIENT\')',
48
            uriTemplate: '/oauth/google/return',
49
            controller: GoogleReturnController::class,
50
        ),
51
        new GetCollection(
52
            security: 'is_granted(\'IS_AUTHENTICATED_ANONYMOUSLY\')',
53
            uriTemplate: '/oauth/mercadolivre/return',
54
            controller: MercadolivreReturnController::class,
55
        ),
56
        new Post(
57
            uriTemplate: '/oauth/google/return',
58
            controller: GoogleReturnController::class,
59
            securityPostDenormalize: 'is_granted(\'ROLE_CLIENT\')',
60
        ),
61
        new Post(
62
            uriTemplate: '/users',
63
            controller: CreateUserAction::class,
64
            securityPostDenormalize: 'is_granted(\'ROLE_CLIENT\')',
65
        ),
66
        new Delete(security: 'is_granted(\'ROLE_CLIENT\')'),
67
68
        new Put(
69
            uriTemplate: '/users/{id}/change-api-key',
70
            controller: ChangeApiKeyAction::class,
71
            securityPostDenormalize: 'is_granted(\'ROLE_CLIENT\')',
72
        ),
73
        new Put(
74
            uriTemplate: '/users/{id}/change-password',
75
            controller: ChangePasswordAction::class,
76
            securityPostDenormalize: 'is_granted(\'ROLE_CLIENT\')',
77
        ),
78
        new Post(
79
            uriTemplate: '/token',
80
            controller: SecurityController::class,
81
            securityPostDenormalize: 'is_granted(\'IS_AUTHENTICATED_ANONYMOUSLY\')',
82
        ),
83
        new Get(security: 'is_granted(\'ROLE_CLIENT\')'),
84
        new GetCollection(security: 'is_granted(\'ROLE_CLIENT\')')
85
    ],
86
    formats: ['jsonld', 'json', 'html', 'jsonhal', 'csv' => ['text/csv']],
87
    normalizationContext: ['groups' => ['user_read']],
88
    denormalizationContext: ['groups' => ['user_write']]
89
)]
90
#[ApiFilter(filterClass: SearchFilter::class, properties: ['people' => 'exact'])]
91
class User implements UserInterface
92
{
93
    /**
94
     * @ORM\Id()
95
     * @ORM\GeneratedValue(strategy="IDENTITY")
96
     * @ORM\Column(type="integer")
97
     * @Groups({"people_read", "user_read"})
98
     */
99
    private $id;
100
    /**
101
     *
102
     * @ORM\Column(type="string", length=50, nullable=false)
103
     * @Groups({"people_read", "user_read"})
104
     */
105
    private $username;
106
    /**
107
     *
108
     * @ORM\Column(type="string", length=255, nullable=false)
109
     */
110
    private $hash;
111
    /**
112
     *
113
     * @ORM\Column(type="string", length=60, nullable=true)
114
     */
115
    private $oauthUser;
116
    /**
117
     *
118
     * @ORM\Column(type="string", length=60, nullable=true)
119
     */
120
    private $oauthHash;
121
    /**
122
     *
123
     * @ORM\Column(type="string", length=60, nullable=true)
124
     */
125
    private $lostPassword;
126
    /**
127
     * @ORM\Column(type="string", length=60, nullable=false)
128
     * @Groups({"people_read", "user_read"})
129
     */
130
    private $apiKey;
131
    /**
132
     * @var \ControleOnline\Entity\People
133
     *
134
     * @ORM\ManyToOne(targetEntity="ControleOnline\Entity\People", inversedBy="user")
135
     * @ORM\JoinColumns({
136
     *   @ORM\JoinColumn(name="people_id", referencedColumnName="id", nullable=false)
137
     * })
138
     * @Groups({"people_read", "user_read"})
139
     */
140
    private $people;
141
    public function __construct()
142
    {
143
        $this->generateApiKey();
144
    }
145
    public function getId(): ?int
146
    {
147
        return $this->id;
148
    }
149
    public function getUsername(): string
150
    {
151
        return (string) $this->username;
152
    }
153
    public function setUsername(string $username): self
154
    {
155
        $this->username = $username;
156
        return $this;
157
    }
158
    /**
159
     * Returns the password used to authenticate the user.
160
     *
161
     * This should be the encoded password. On authentication, a plain-text
162
     * password will be salted, encoded, and then compared to this value.
163
     *
164
     * @return string|null The encoded password if any
165
     */
166
    public function getPassword()
167
    {
168
        return $this->hash;
169
    }
170
    /**
171
     * Returns the roles granted to the user.
172
     *
173
     *     public function getRoles()
174
     *     {
175
     *         return ['ROLE_USER'];
176
     *     }
177
     *
178
     * Alternatively, the roles might be stored on a ``roles`` property,
179
     * and populated in any number of different ways when the user object
180
     * is created.
181
     *
182
     * @return string[] The user roles
183
     */
184
    public function getRoles()
185
    {
186
        return ['ROLE_CLIENT'];
187
    }
188
    /**
189
     * @see UserInterface
190
     */
191
    public function getSalt()
192
    {
193
        // not needed when using the "bcrypt" algorithm in security.yaml
194
    }
195
    /**
196
     * @see UserInterface
197
     */
198
    public function eraseCredentials()
199
    {
200
        // If you store any temporary, sensitive data on the user, clear it here
201
        // $this->plainPassword = null;
202
    }
203
204
    public function generateApiKey(): string
205
    {
206
        $this->apiKey = md5($this->getUsername() . microtime());
207
        return $this->apiKey;
208
    }
209
210
    public function getApiKey(): string
211
    {
212
        return $this->apiKey;
213
    }
214
    public function setHash($hash): self
215
    {
216
        $this->hash = $hash;
217
        return $this;
218
    }
219
    public function getHash(): string
220
    {
221
        return $this->hash;
222
    }
223
    public function getOauthHash(): string
224
    {
225
        return $this->oauthHash;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->oauthHash could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
226
    }
227
    public function setOauthHash($hash): self
228
    {
229
        $this->oauthHash = $hash;
230
        return $this;
231
    }
232
    public function setOauthUser($user): self
233
    {
234
        $this->oauthUser = $user;
235
        return $this;
236
    }
237
    public function getOauthUser(): string
238
    {
239
        return $this->oauthUser;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->oauthUser could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
240
    }
241
    public function setPeople(People $people = null): self
242
    {
243
        $this->people = $people;
244
        return $this;
245
    }
246
    public function getPeople(): People
247
    {
248
        return $this->people;
249
    }
250
    public function setLostPassword(?string $hash): self
251
    {
252
        $this->lostPassword = $hash;
253
        return $this;
254
    }
255
    public function getLostPassword(): ?string
256
    {
257
        return $this->lostPassword;
258
    }
259
}
260