1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\ORM\Mapping as ORM; |
7
|
|
|
use Gedmo\Mapping\Annotation as Gedmo; |
8
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
9
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* User |
13
|
|
|
* |
14
|
|
|
* @ORM\Table(name="user") |
15
|
|
|
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository") |
16
|
|
|
* @ORM\HasLifecycleCallbacks |
17
|
|
|
*/ |
18
|
|
|
class User |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
* |
23
|
|
|
* @ORM\Column(name="id", type="integer") |
24
|
|
|
* @ORM\Id |
25
|
|
|
* @ORM\GeneratedValue(strategy="AUTO") |
26
|
|
|
*/ |
27
|
|
|
private $id; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
* |
32
|
|
|
* @ORM\Column(name="name", type="string", length=50, unique=true) |
33
|
|
|
* |
34
|
|
|
* @Assert\NotBlank() |
35
|
|
|
* @Assert\Length(max = 50) |
36
|
|
|
*/ |
37
|
|
|
private $name; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
* |
42
|
|
|
* @Gedmo\Slug(fields={"name"}, updatable=true, separator="_") |
43
|
|
|
* @ORM\Column(name="slug", type="string", length=50, unique=true) |
44
|
|
|
* |
45
|
|
|
* @Assert\Length(max = 50) |
46
|
|
|
*/ |
47
|
|
|
private $slug; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
* |
52
|
|
|
* @ORM\Column(name="photo", type="string", length=150, nullable=true) |
53
|
|
|
* |
54
|
|
|
* @Assert\Length(max = 150) |
55
|
|
|
*/ |
56
|
|
|
private $photo; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @Assert\File(maxSize="6000000", |
60
|
|
|
* mimeTypes={ |
61
|
|
|
* "image/jpeg", |
62
|
|
|
* "image/jpg", |
63
|
|
|
* "image/png"}) |
64
|
|
|
*/ |
65
|
|
|
private $file; |
66
|
|
|
|
67
|
|
|
private $temp; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string |
71
|
|
|
* |
72
|
|
|
* @ORM\Column(name="email", type="string", length=50) |
73
|
|
|
* |
74
|
|
|
* @Assert\NotBlank() |
75
|
|
|
* @Assert\Email() |
76
|
|
|
*/ |
77
|
|
|
private $email; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var string |
81
|
|
|
* |
82
|
|
|
* @ORM\Column(name="password", type="string", length=100) |
83
|
|
|
* |
84
|
|
|
* @Assert\NotBlank() |
85
|
|
|
* @Assert\Length(max = 100) |
86
|
|
|
*/ |
87
|
|
|
private $password; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @ORM\OneToMany(targetEntity="Article", mappedBy="user") |
91
|
|
|
*/ |
92
|
|
|
private $articles; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @ORM\OneToMany(targetEntity="Comment", mappedBy="user") |
96
|
|
|
*/ |
97
|
|
|
private $comments; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @ORM\ManyToOne(targetEntity="Role", inversedBy="users") |
101
|
|
|
* @ORM\JoinColumn(name="role_id", referencedColumnName="id") |
102
|
|
|
*/ |
103
|
|
|
private $role; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @var \DateTime |
107
|
|
|
* |
108
|
|
|
* @Gedmo\Timestampable(on="create") |
109
|
|
|
* @ORM\Column(name="created_at", type="datetime") |
110
|
|
|
* |
111
|
|
|
* @Assert\DateTime() |
112
|
|
|
*/ |
113
|
|
|
private $createdAt; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @var \DateTime |
117
|
|
|
* |
118
|
|
|
* @Gedmo\Timestampable(on="change", field={"user", "role", "email", "password"}) |
119
|
|
|
* @ORM\Column(name="updated_at", type="datetime", nullable=true) |
120
|
|
|
* |
121
|
|
|
* @Assert\DateTime() |
122
|
|
|
*/ |
123
|
|
|
private $updatedAt; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @var \DateTime |
127
|
|
|
* |
128
|
|
|
* @ORM\Column(name="deleted_at", type="datetime", nullable=true) |
129
|
|
|
* |
130
|
|
|
* @Assert\DateTime() |
131
|
|
|
*/ |
132
|
|
|
private $deletedAt; |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get id |
137
|
|
|
* |
138
|
|
|
* @return int |
139
|
|
|
*/ |
140
|
|
|
public function getId() |
141
|
|
|
{ |
142
|
|
|
return $this->id; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Set user |
147
|
|
|
* |
148
|
|
|
* @param string $name |
149
|
|
|
* |
150
|
|
|
* @return User |
151
|
|
|
*/ |
152
|
|
|
public function setName($name) |
153
|
|
|
{ |
154
|
|
|
$this->name = $name; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get user |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
7 |
|
public function getName() |
165
|
|
|
{ |
166
|
7 |
|
return $this->name; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Set slug |
171
|
|
|
* |
172
|
|
|
* @param string $slug |
173
|
|
|
* |
174
|
|
|
* @return User |
175
|
|
|
*/ |
176
|
|
|
public function setSlug($slug) |
177
|
|
|
{ |
178
|
|
|
$this->slug = $slug; |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get slug |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
6 |
|
public function getSlug() |
189
|
|
|
{ |
190
|
6 |
|
return $this->slug; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Set email |
195
|
|
|
* |
196
|
|
|
* @param string $email |
197
|
|
|
* |
198
|
|
|
* @return User |
199
|
|
|
*/ |
200
|
|
|
public function setEmail($email) |
201
|
|
|
{ |
202
|
|
|
$this->email = $email; |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get email |
209
|
|
|
* |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
public function getEmail() |
213
|
|
|
{ |
214
|
|
|
return $this->email; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Set createdAt |
219
|
|
|
* |
220
|
|
|
* @param \DateTime $createdAt |
221
|
|
|
* |
222
|
|
|
* @return User |
223
|
|
|
*/ |
224
|
|
|
public function setCreatedAt($createdAt) |
225
|
|
|
{ |
226
|
|
|
$this->createdAt = $createdAt; |
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get createdAt |
233
|
|
|
* |
234
|
|
|
* @return \DateTime |
235
|
|
|
*/ |
236
|
|
|
public function getCreatedAt() |
237
|
|
|
{ |
238
|
|
|
return $this->createdAt; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set updatedAt |
243
|
|
|
* |
244
|
|
|
* @param \DateTime $updatedAt |
245
|
|
|
* |
246
|
|
|
* @return User |
247
|
|
|
*/ |
248
|
|
|
public function setUpdatedAt($updatedAt) |
249
|
|
|
{ |
250
|
|
|
$this->updatedAt = $updatedAt; |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get updatedAt |
257
|
|
|
* |
258
|
|
|
* @return \DateTime |
259
|
|
|
*/ |
260
|
|
|
public function getUpdatedAt() |
261
|
|
|
{ |
262
|
|
|
return $this->updatedAt; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Set deletedAt |
267
|
|
|
* |
268
|
|
|
* @param \DateTime $deletedAt |
269
|
|
|
* |
270
|
|
|
* @return User |
271
|
|
|
*/ |
272
|
|
|
public function setDeletedAt($deletedAt) |
273
|
|
|
{ |
274
|
|
|
$this->deletedAt = $deletedAt; |
275
|
|
|
|
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Get deletedAt |
281
|
|
|
* |
282
|
|
|
* @return \DateTime |
283
|
|
|
*/ |
284
|
|
|
public function getDeletedAt() |
285
|
|
|
{ |
286
|
|
|
return $this->deletedAt; |
287
|
|
|
} |
288
|
|
|
/** |
289
|
|
|
* Constructor |
290
|
|
|
*/ |
291
|
|
|
public function __construct() |
292
|
|
|
{ |
293
|
|
|
$this->articles = new ArrayCollection(); |
294
|
|
|
$this->comments = new ArrayCollection(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Add article |
299
|
|
|
* |
300
|
|
|
* @param \AppBundle\Entity\Article $article |
301
|
|
|
* |
302
|
|
|
* @return User |
303
|
|
|
*/ |
304
|
|
|
public function addArticle(Article $article) |
305
|
|
|
{ |
306
|
|
|
$this->articles[] = $article; |
307
|
|
|
|
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Remove article |
313
|
|
|
* |
314
|
|
|
* @param \AppBundle\Entity\Article $article |
315
|
|
|
*/ |
316
|
|
|
public function removeArticle(Article $article) |
317
|
|
|
{ |
318
|
|
|
$this->articles->removeElement($article); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Get articles |
323
|
|
|
* |
324
|
|
|
* @return \Doctrine\Common\Collections\Collection |
325
|
|
|
*/ |
326
|
|
|
public function getArticles() |
327
|
|
|
{ |
328
|
|
|
return $this->articles; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Add comment |
333
|
|
|
* |
334
|
|
|
* @param \AppBundle\Entity\Comment $comment |
335
|
|
|
* |
336
|
|
|
* @return User |
337
|
|
|
*/ |
338
|
|
|
public function addComment(Comment $comment) |
339
|
|
|
{ |
340
|
|
|
$this->comments[] = $comment; |
341
|
|
|
|
342
|
|
|
return $this; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Remove comment |
347
|
|
|
* |
348
|
|
|
* @param \AppBundle\Entity\Comment $comment |
349
|
|
|
*/ |
350
|
|
|
public function removeComment(Comment $comment) |
351
|
|
|
{ |
352
|
|
|
$this->comments->removeElement($comment); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Get comments |
357
|
|
|
* |
358
|
|
|
* @return \Doctrine\Common\Collections\Collection |
359
|
|
|
*/ |
360
|
|
|
public function getComments() |
361
|
|
|
{ |
362
|
|
|
return $this->comments; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Set role |
367
|
|
|
* |
368
|
|
|
* @param \AppBundle\Entity\Role $role |
369
|
|
|
* |
370
|
|
|
* @return User |
371
|
|
|
*/ |
372
|
|
|
public function setRole(Role $role = null) |
373
|
|
|
{ |
374
|
|
|
$this->role = $role; |
375
|
|
|
|
376
|
|
|
return $this; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Get role |
381
|
|
|
* |
382
|
|
|
* @return \AppBundle\Entity\Role |
383
|
|
|
*/ |
384
|
|
|
public function getRole() |
385
|
|
|
{ |
386
|
|
|
return $this->role; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Set password |
391
|
|
|
* |
392
|
|
|
* @param string $password |
393
|
|
|
* |
394
|
|
|
* @return User |
395
|
|
|
*/ |
396
|
|
|
public function setPassword($password) |
397
|
|
|
{ |
398
|
|
|
$this->password = $password; |
399
|
|
|
|
400
|
|
|
return $this; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Get password |
405
|
|
|
* |
406
|
|
|
* @return string |
407
|
|
|
*/ |
408
|
|
|
public function getPassword() |
409
|
|
|
{ |
410
|
|
|
return $this->password; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Set photo |
415
|
|
|
* |
416
|
|
|
* @param string $photo |
417
|
|
|
* |
418
|
|
|
* @return User |
419
|
|
|
*/ |
420
|
|
|
public function setPhoto($photo) |
421
|
|
|
{ |
422
|
|
|
$this->photo = $photo; |
423
|
|
|
|
424
|
|
|
return $this; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Get photo |
429
|
|
|
* |
430
|
|
|
* @return string |
431
|
|
|
*/ |
432
|
6 |
|
public function getPhoto() |
433
|
|
|
{ |
434
|
6 |
|
return $this->photo; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* *********************** |
440
|
|
|
* Upload images start |
441
|
|
|
* *********************** |
442
|
|
|
*/ |
443
|
|
|
|
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Sets file. |
447
|
|
|
* |
448
|
|
|
* @param UploadedFile $file |
449
|
|
|
*/ |
450
|
|
View Code Duplication |
public function setFile(UploadedFile $file = null) |
|
|
|
|
451
|
|
|
{ |
452
|
|
|
$this->file = $file; |
453
|
|
|
if (isset($this->photo)) { |
454
|
|
|
$this->temp = $this->photo; |
455
|
|
|
$this->photo = null; |
456
|
|
|
} else { |
457
|
|
|
$this->photo = 'initial'; |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Get file. |
463
|
|
|
* |
464
|
|
|
* @return UploadedFile |
465
|
|
|
*/ |
466
|
|
|
public function getFile() |
467
|
|
|
{ |
468
|
|
|
return $this->file; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
public function getAbsolutePath() |
472
|
|
|
{ |
473
|
|
|
return null === $this->photo |
474
|
|
|
? null |
475
|
|
|
: $this->getUploadRootDir().'/'.$this->photo; |
476
|
|
|
} |
477
|
|
|
|
478
|
6 |
|
public function getWebPath() |
479
|
|
|
{ |
480
|
6 |
|
return null === $this->photo |
481
|
6 |
|
? null |
482
|
6 |
|
: $this->getUploadDir().'/'.$this->photo; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
protected function getUploadRootDir() |
486
|
|
|
{ |
487
|
|
|
return __DIR__.'/../../../web/'.$this->getUploadDir(); |
488
|
|
|
} |
489
|
|
|
|
490
|
6 |
|
protected function getUploadDir() |
491
|
|
|
{ |
492
|
6 |
|
return 'media/users'; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* @ORM\PrePersist() |
497
|
|
|
* @ORM\PreUpdate() |
498
|
|
|
*/ |
499
|
|
View Code Duplication |
public function preUpload() |
|
|
|
|
500
|
|
|
{ |
501
|
|
|
if (null !== $this->getFile()) { |
502
|
|
|
$filename = sha1(uniqid(mt_rand(), true)); |
503
|
|
|
$this->photo = $filename.'.'.$this->getFile()->guessExtension(); |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* @ORM\PostPersist() |
509
|
|
|
* @ORM\PostUpdate() |
510
|
|
|
*/ |
511
|
|
View Code Duplication |
public function upload() |
|
|
|
|
512
|
|
|
{ |
513
|
|
|
if (null === $this->getFile()) { |
514
|
|
|
return; |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
$this->getFile()->move($this->getUploadRootDir(), $this->photo); |
518
|
|
|
|
519
|
|
|
if (isset($this->temp)) { |
520
|
|
|
unlink($this->getUploadRootDir().'/'.$this->temp); |
521
|
|
|
|
522
|
|
|
$this->clearCache($this->getUploadDir().'/'.$this->temp); |
523
|
|
|
|
524
|
|
|
$this->temp = null; |
525
|
|
|
} |
526
|
|
|
$this->file = null; |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
/** |
530
|
|
|
* @ORM\PostRemove() |
531
|
|
|
*/ |
532
|
|
|
public function removeUpload() |
533
|
|
|
{ |
534
|
|
|
$file = $this->getAbsolutePath(); |
535
|
|
|
if ($file) { |
536
|
|
|
unlink($file); |
537
|
|
|
|
538
|
|
|
$this->clearCache($this->getWebPath()); |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
public function clearCache($path) |
|
|
|
|
543
|
|
|
{ |
544
|
|
|
$kernel = $GLOBALS['kernel']; |
545
|
|
|
$cacheManager = $kernel->getContainer()->get('liip_imagine.cache.manager'); |
546
|
|
|
$cacheManager->remove($path); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* *********************** |
551
|
|
|
* Upload images end |
552
|
|
|
* *********************** |
553
|
|
|
*/ |
554
|
|
|
} |
555
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.