Passed
Push — issue#785 ( 73515d...f798de )
by Guilherme
04:17
created

Person::getFacebookUsername()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Entity;
12
13
use Doctrine\ORM\Mapping as ORM;
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use libphonenumber\PhoneNumber;
17
use LoginCidadao\BadgesControlBundle\Model\BadgeInterface;
18
use Symfony\Component\HttpFoundation\File\File;
19
use Symfony\Component\Validator\Constraints as Assert;
20
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
21
use Scheb\TwoFactorBundle\Model\BackupCodeInterface;
22
use Vich\UploaderBundle\Mapping\Annotation as Vich;
23
use JMS\Serializer\Annotation as JMS;
24
use FOS\UserBundle\Model\User as BaseUser;
25
use LoginCidadao\OAuthBundle\Entity\Client;
26
use LoginCidadao\CoreBundle\Model\LocationSelectData;
27
use LoginCidadao\CoreBundle\Model\PersonInterface;
28
use LoginCidadao\OAuthBundle\Model\ClientInterface;
29
use LoginCidadao\ValidationBundle\Validator\Constraints as LCAssert;
30
use Donato\PathWellBundle\Validator\Constraints\PathWell;
31
use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;
32
use Rollerworks\Bundle\PasswordStrengthBundle\Validator\Constraints as RollerworksPassword;
33
34
/**
35
 * @ORM\Entity(repositoryClass="LoginCidadao\CoreBundle\Entity\PersonRepository")
36
 * @ORM\Table(name="person")
37
 * @UniqueEntity("cpf", message="person.validation.cpf.already_used", groups={"LoginCidadaoRegistration", "Registration", "Profile", "LoginCidadaoProfile", "Dynamic", "Documents"})
38
 * @UniqueEntity("username")
39
 * @UniqueEntity(fields="emailCanonical", errorPath="email", message="fos_user.email.already_used", groups={"LoginCidadaoRegistration", "Registration", "LoginCidadaoEmailForm", "LoginCidadaoProfile", "Dynamic"})
40
 * @ORM\HasLifecycleCallbacks
41
 * @JMS\ExclusionPolicy("all")
42
 * @Vich\Uploadable
43
 */
44
class Person extends BaseUser implements PersonInterface, BackupCodeInterface
45
{
46
    /**
47
     * @ORM\Id
48
     * @ORM\Column(type="integer")
49
     * @ORM\GeneratedValue(strategy="AUTO")
50
     * @JMS\Since("1.0")
51
     * @JMS\Until("2")
52
     */
53
    protected $id;
54
55
    /**
56
     * @JMS\Expose
57
     * @JMS\Groups({"first_name","full_name","public_profile","given_name","name"})
58
     * @ORM\Column(type="string", nullable=true)
59
     * @Assert\NotBlank(message="Please enter your name.", groups={"Profile", "LoginCidadaoProfile"})
60
     * @Assert\Length(
61
     *     min=3,
62
     *     max="255",
63
     *     minMessage="The name is too short.",
64
     *     maxMessage="The name is too long.",
65
     *     groups={"Registration", "Profile", "LoginCidadaoProfile"}
66
     * )
67
     * @JMS\Since("1.0")
68
     */
69
    protected $firstName;
70
71
    /**
72
     * @JMS\Expose
73
     * @JMS\Groups({"last_name","full_name","family_name","middle_name","name"})
74
     * @ORM\Column(type="string", nullable=true)
75
     * @Assert\NotBlank(message="Please enter your surname.", groups={"Profile", "LoginCidadaoProfile"})
76
     * @Assert\Length(
77
     *     min=1,
78
     *     max="255",
79
     *     minMessage="The surname is too short.",
80
     *     maxMessage="The surname is too long.",
81
     *     groups={"Registration", "Profile", "LoginCidadaoProfile"}
82
     * )
83
     * @JMS\Since("1.0")
84
     */
85
    protected $surname;
86
87
    /**
88
     * @JMS\Expose
89
     * @JMS\Groups({"username","preferred_username"})
90
     * @Assert\NotBlank
91
     * @Assert\Length(
92
     *     min="1",
93
     *     max="40",
94
     *     groups={"Registration", "Profile", "LoginCidadaoProfile"}
95
     * )
96
     * @JMS\Since("1.0")
97
     */
98
    protected $username;
99
100
    /**
101
     * @JMS\Exclude
102
     * @PathWell(
103
     *     groups={"Registration", "ResetPassword", "ChangePassword", "LoginCidadaoRegistration"}
104
     * )
105
     * @RollerworksPassword\PasswordRequirements(
106
     *     minLength=false,
107
     *     requireLetters=true,
108
     *     requireNumbers=true,
109
     *     missingLettersMessage="person.validation.password.missingLetters",
110
     *     missingNumbersMessage="person.validation.password.missingNumbers",
111
     *     groups={"Registration", "ResetPassword", "ChangePassword", "LoginCidadaoRegistration"}
112
     * )
113
     * @Assert\Length(
114
     *     min=8,
115
     *     max=72,
116
     *     maxMessage="person.validation.password.length.max",
117
     *     minMessage="person.validation.password.length.min",
118
     *     groups={"Registration", "ResetPassword", "ChangePassword", "LoginCidadaoRegistration"}
119
     * )
120
     * @Assert\NotBlank(message="person.validation.password.not_blank", groups={"Registration", "ResetPassword", "ChangePassword", "LoginCidadaoRegistration"})
121
     */
122
    protected $plainPassword;
123
124
    /**
125
     * @JMS\Expose
126
     * @JMS\Groups({"cpf"})
127
     * @ORM\Column(type="string", nullable=true, unique=true)
128
     * @LCAssert\CPF(groups={"Documents", "Dynamic", "LoginCidadaoRegistration"})
129
     * @JMS\Since("1.0")
130
     */
131
    protected $cpf;
132
133
    /**
134
     * @JMS\Expose
135
     * @JMS\Groups({"email"})
136
     * @JMS\Since("1.0")
137
     * @LCAssert\Email(strict=true, groups={"Profile", "LoginCidadaoProfile", "Registration", "ResetPassword", "ChangePassword", "LoginCidadaoRegistration", "LoginCidadaoEmailForm"})
138
     * @Assert\NotBlank(message="person.validation.email.not_blank", groups={"Profile", "LoginCidadaoProfile", "Registration", "ResetPassword", "ChangePassword", "LoginCidadaoRegistration", "LoginCidadaoEmailForm"})
139
     */
140
    protected $email;
141
142
    /**
143
     * @JMS\Expose
144
     * @JMS\Groups({"birthdate"})
145
     * @ORM\Column(type="date", nullable=true)
146
     * @JMS\Since("1.0")
147
     * @LCAssert\Age(max="150", groups={"Profile", "LoginCidadaoProfile", "Registration", "ResetPassword", "ChangePassword", "LoginCidadaoRegistration", "LoginCidadaoEmailForm"})
148
     */
149
    protected $birthdate;
150
151
    /**
152
     * @ORM\Column(name="email_expiration", type="datetime", nullable=true)
153
     * @JMS\Since("1.0")
154
     */
155
    protected $emailExpiration;
156
157
    /**
158
     * @JMS\Expose
159
     * @JMS\Groups({"mobile","phone_number"})
160
     * @JMS\Type("libphonenumber\PhoneNumber")
161
     * @ORM\Column(type="phone_number", nullable=true)
162
     * @JMS\Since("1.0")
163
     * @LCAssert\E164PhoneNumber(
164
     *     maxMessage="person.validation.mobile.length.max",
165
     *     groups={"Registration", "LoginCidadaoRegistration", "Dynamic", "Profile", "LoginCidadaoProfile"}
166
     * )
167
     * @LCAssert\MobilePhoneNumber(
168
     *     missing9thDigit="person.validation.mobile.9thDigit",
169
     *     groups={"Registration", "LoginCidadaoRegistration", "Dynamic", "Profile", "LoginCidadaoProfile"}
170
     * )
171
     * @AssertPhoneNumber(
172
     *     type="mobile",
173
     *     groups={"Registration", "LoginCidadaoRegistration", "Dynamic", "Profile", "LoginCidadaoProfile"}
174
     * )
175
     */
176
    protected $mobile;
177
178
    /**
179
     * @ORM\Column(type="string", length=255, nullable=true)
180
     * @var string
181
     * @JMS\Since("1.0")
182
     */
183
    protected $twitterPicture;
184
185
    /**
186
     * @JMS\Expose
187
     * @JMS\Groups({"city"})
188
     * @ORM\ManyToOne(targetEntity="LoginCidadao\CoreBundle\Entity\City")
189
     * @ORM\JoinColumn(name="city_id", referencedColumnName="id")
190
     * @JMS\Since("1.0")
191
     */
192
    protected $city;
193
194
    /**
195
     * @var string
196
     *
197
     * @ORM\Column(name="facebookId", type="string", length=255, nullable=true, unique=true)
198
     * @JMS\Exclude
199
     */
200
    protected $facebookId;
201
202
    /**
203
     * @var string
204
     *
205
     * @ORM\Column(name="facebookUsername", type="string", length=255, nullable=true)
206
     * @JMS\Exclude
207
     */
208
    protected $facebookUsername;
209
210
    /**
211
     * @var string
212
     *
213
     * @ORM\Column(name="facebookAccessToken", type="string", length=255, nullable=true)
214
     * @JMS\Exclude()
215
     */
216
    protected $facebookAccessToken;
217
218
    /**
219
     * @var string
220
     *
221
     * @ORM\Column(name="twitterId", type="string", length=255, nullable=true, unique=true)
222
     * @JMS\Exclude
223
     */
224
    protected $twitterId;
225
226
    /**
227
     * @var string
228
     *
229
     * @ORM\Column(name="twitterUsername", type="string", length=255, nullable=true)
230
     * @JMS\Exclude
231
     */
232
    protected $twitterUsername;
233
234
    /**
235
     * @var string
236
     *
237
     * @ORM\Column(name="twitterAccessToken", type="string", length=255, nullable=true)
238
     * @JMS\Exclude
239
     */
240
    protected $twitterAccessToken;
241
242
    /**
243
     * @ORM\OneToMany(targetEntity="Authorization", mappedBy="person", cascade={"remove"}, orphanRemoval=true)
244
     */
245
    protected $authorizations;
246
247
    /**
248
     * @ORM\Column(type="datetime", nullable=false)
249
     * @var \DateTime
250
     * @JMS\Since("1.0")
251
     */
252
    protected $createdAt;
253
254
    /**
255
     * @ORM\Column(type="datetime", nullable=true)
256
     * @var \DateTime
257
     * @JMS\Since("1.0")
258
     */
259
    protected $emailConfirmedAt;
260
261
    /**
262
     * @ORM\Column(type="string", length=255, nullable=true)
263
     * @var string
264
     * @JMS\Since("1.0")
265
     */
266
    protected $previousValidEmail;
267
268
    /**
269
     * @ORM\ManyToMany(targetEntity="LoginCidadao\OAuthBundle\Entity\Client", mappedBy="owners")
270
     */
271
    protected $clients;
272
273
    /**
274
     * @ORM\OneToMany(targetEntity="ClientSuggestion", mappedBy="person")
275
     */
276
    protected $suggestions;
277
278
    /**
279
     * @JMS\Expose
280
     * @JMS\Groups({"state"})
281
     * @ORM\ManyToOne(targetEntity="LoginCidadao\CoreBundle\Entity\State")
282
     * @ORM\JoinColumn(name="state_id", referencedColumnName="id")
283
     * @JMS\Since("1.0.2")
284
     */
285
    protected $state;
286
287
    /**
288
     * @JMS\Expose
289
     * @JMS\Groups({"country"})
290
     * @ORM\ManyToOne(targetEntity="LoginCidadao\CoreBundle\Entity\Country")
291
     * @ORM\JoinColumn(name="country_id", referencedColumnName="id")
292
     * @JMS\Since("1.0.2")
293
     */
294
    protected $country;
295
296
    /**
297
     * @Assert\File(
298
     *      maxSize="2M",
299
     *      maxSizeMessage="The maxmimum allowed file size is 2MB.",
300
     *      mimeTypes={"image/png", "image/jpeg", "image/pjpeg"},
301
     *      mimeTypesMessage="Only JPEG and PNG images are allowed."
302
     * )
303
     * @Vich\UploadableField(mapping="user_image", fileNameProperty="imageName")
304
     * @var File $image
305
     * @JMS\Since("1.0.2")
306
     */
307
    protected $image;
308
309
    /**
310
     * @ORM\Column(type="string", length=255, name="image_name", nullable=true)
311
     *
312
     * @var string $imageName
313
     * @JMS\Since("1.0.2")
314
     */
315
    protected $imageName;
316
317
    /**
318
     * @JMS\Expose
319
     * @JMS\Groups({"public_profile","picture"})
320
     * @JMS\Since("1.0.2")
321
     * @JMS\Until("2")
322
     */
323
    protected $profilePictureUrl;
324
325
    /**
326
     * @ORM\Column(type="datetime", nullable=true)
327
     * @JMS\Expose
328
     * @JMS\Groups({"public_profile","updated_at"})
329
     * @var \DateTime $updatedAt
330
     * @JMS\Since("1.0.2")
331
     */
332
    protected $updatedAt;
333
334
    /**
335
     * @var string
336
     *
337
     * @ORM\Column(name="googleId", type="string", length=255, nullable=true, unique=true)
338
     * @JMS\Exclude
339
     */
340
    protected $googleId;
341
342
    /**
343
     * @var string
344
     *
345
     * @ORM\Column(name="googleUsername", type="string", length=255, nullable=true)
346
     * @JMS\Exclude
347
     */
348
    protected $googleUsername;
349
350
    /**
351
     * @var string
352
     *
353
     * @ORM\Column(name="googleAccessToken", type="string", length=255, nullable=true)
354
     * @JMS\Exclude
355
     */
356
    protected $googleAccessToken;
357
358
    /**
359
     * @JMS\Expose
360
     * @JMS\Groups({"id_cards"})
361
     * @ORM\OneToMany(targetEntity="LoginCidadao\CoreBundle\Entity\IdCard", mappedBy="person")
362
     * @JMS\Since("1.0.3")
363
     */
364
    protected $idCards;
365
366
    /**
367
     * @JMS\Expose
368
     * @JMS\Groups({"public_profile"})
369
     * @JMS\Type("array<LoginCidadao\BadgesControlBundle\Model\Badge>")
370
     * @JMS\Since("2")
371
     * @JMS\Until("3")
372
     * @deprecated Use RemoteClaims instead
373
     * @var array|BadgeInterface[]
374
     */
375
    protected $badges = [];
376
377
    /**
378
     * @ORM\OneToMany(targetEntity="LoginCidadao\APIBundle\Entity\LogoutKey", mappedBy="person", cascade={"remove"}, orphanRemoval=true)
379
     */
380
    protected $logoutKeys;
381
382
    /**
383
     * @JMS\Expose
384
     * @JMS\Groups({"addresses","address"})
385
     * @ORM\OneToMany(targetEntity="LoginCidadao\CoreBundle\Entity\PersonAddress", mappedBy="person", cascade={"remove"}, orphanRemoval=true)
386
     */
387
    protected $addresses;
388
389
    /**
390
     * @ORM\Column(name="google_authenticator_secret", type="string", nullable=true)
391
     * @JMS\Exclude
392
     */
393
    protected $googleAuthenticatorSecret;
394
395
    /**
396
     * @JMS\Expose
397
     * @JMS\Groups({"nationality"})
398
     * @ORM\ManyToOne(targetEntity="LoginCidadao\CoreBundle\Entity\Country")
399
     * @ORM\JoinColumn(name="nationality_id", referencedColumnName="id")
400
     * @JMS\Since("1.0.2")
401
     */
402
    protected $nationality;
403
404
    /**
405
     * @JMS\Exclude
406
     * @ORM\OneToMany(targetEntity="BackupCode", mappedBy="person", cascade={"remove"}, orphanRemoval=true)
407
     */
408
    protected $backupCodes;
409
410
    /**
411
     * @JMS\Exclude
412
     * @ORM\Column(name="password_encoder_name", type="string", length=255, nullable=true)
413
     */
414
    protected $passwordEncoderName;
415
416
    /**
417
     * @JMS\Expose
418
     * @JMS\Groups({"public_profile"})
419
     * @JMS\SerializedName("phone_number_verified")
420
     * @JMS\Since("1.1")
421
     * @var bool
422
     */
423
    protected $phoneNumberVerified = false;
424
425 43
    public function __construct()
426
    {
427 43
        parent::__construct();
428 43
        $this->authorizations = new ArrayCollection();
429 43
        $this->clients = new ArrayCollection();
430 43
        $this->logoutKeys = new ArrayCollection();
431 43
        $this->addresses = new ArrayCollection();
432 43
        $this->backupCodes = new ArrayCollection();
433 43
    }
434
435 2
    public function getEmail()
436
    {
437 2
        return $this->email;
438
    }
439
440 3
    public function setEmail($email)
441
    {
442 3
        $this->email = $email;
443
444 3
        return $this;
445
    }
446
447 3
    public function getFirstName()
448
    {
449 3
        return $this->firstName;
450
    }
451
452 1
    public function setFirstName($firstName)
453
    {
454 1
        $this->firstName = $firstName;
455
456 1
        return $this;
457
    }
458
459 3
    public function getSurname()
460
    {
461 3
        return $this->surname;
462
    }
463
464 1
    public function setSurname($suname)
465
    {
466 1
        $this->surname = $suname;
467
468 1
        return $this;
469
    }
470
471 2
    public function getBirthdate()
472
    {
473 2
        return $this->birthdate;
474
    }
475
476
    public function setBirthdate($birthdate)
477
    {
478
        $this->birthdate = $birthdate;
479
480
        return $this;
481
    }
482
483 9
    public function getMobile()
484
    {
485 9
        return $this->mobile;
486
    }
487
488 6
    public function setMobile($mobile)
489
    {
490 6
        if (!($mobile instanceof PhoneNumber)) {
491 1
            $mobile = preg_replace('/[^0-9+]/', '', $mobile);
492
493
            // PhoneNumberBundle won't work with empty strings.
494
            // See https://github.com/misd-service-development/phone-number-bundle/issues/58
495 1
            if (strlen(trim($mobile)) === 0) {
496 1
                $mobile = null;
497
            }
498
        }
499 6
        $this->mobile = $mobile;
500
501 6
        return $this;
502
    }
503
504
    public function addAuthorization(Authorization $authorization)
505
    {
506
        $this->authorizations->add($authorization);
507
        $authorization->setPerson($this);
508
509
        return $this;
510
    }
511
512
    public function removeAuthorization(Authorization $authorization)
513
    {
514
        if ($this->authorizations->contains($authorization)) {
515
            $this->authorizations->removeElement($authorization);
516
        }
517
518
        return $this;
519
    }
520
521
    /**
522
     * @return Authorization[]
523
     */
524
    public function getAuthorizations($uidToIgnore = null)
525
    {
526
        if ($uidToIgnore !== null) {
527
            return array_filter(
528
                $this->authorizations->toArray(),
529
                function (Authorization $authorization) use ($uidToIgnore) {
530
                    return $authorization->getClient()->getUid() != $uidToIgnore;
531
                }
532
            );
533
        }
534
535
        return $this->authorizations;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->authorizations returns the type Doctrine\Common\Collections\ArrayCollection which is incompatible with the documented return type LoginCidadao\CoreBundle\Entity\Authorization[].
Loading history...
536
    }
537
538
    /**
539
     * Checks if a given Client can access this Person's specified scope.
540
     * @param ClientInterface $client
541
     * @param mixed $scope can be a single scope or an array with several.
542
     * @return boolean
543
     */
544
    public function isAuthorizedClient(ClientInterface $client, $scope)
545
    {
546
        $authorizations = $this->getAuthorizations();
547
        foreach ($authorizations as $auth) {
548
            $c = $auth->getClient();
549
            if ($c->getId() == $client->getId()) {
550
                return $auth->hasScopes($scope);
551
            }
552
        }
553
554
        return false;
555
    }
556
557
    /**
558
     * @param Client $client
559
     * @return array
560
     */
561
    public function getClientScope(Client $client)
562
    {
563
        $authorizations = $this->getAuthorizations();
564
        foreach ($authorizations as $auth) {
565
            $c = $auth->getClient();
566
            if ($c->getId() == $client->getId()) {
567
                return $auth->getScope();
568
            }
569
        }
570
571
        return null;
572
    }
573
574
    /**
575
     * Checks if this Person has any authorization for a given Client.
576
     * WARNING: Note that it does NOT validate scope!
577
     * @param \LoginCidadao\OAuthBundle\Entity\Client | integer $client
578
     */
579
    public function hasAuthorization($client)
580
    {
581
        if ($client instanceof ClientInterface) {
582
            $id = $client->getId();
583
        } else {
584
            $id = $client;
585
        }
586
        $authorizations = $this->getAuthorizations();
587
        if (is_array($authorizations) || $authorizations instanceof Collection) {
0 ignored issues
show
introduced by
The condition is_array($authorizations) is always true.
Loading history...
588
            foreach ($authorizations as $auth) {
589
                $c = $auth->getClient();
590
                if ($c->getId() == $id) {
591
                    return true;
592
                }
593
            }
594
        }
595
596
        return false;
597
    }
598
599
    public function setFacebookId($facebookId)
600
    {
601
        $this->facebookId = $facebookId;
602
603
        return $this;
604
    }
605
606
    public function getFacebookId()
607
    {
608
        return $this->facebookId;
609
    }
610
611
    public function setTwitterId($twitterId)
612
    {
613
        $this->twitterId = $twitterId;
614
615
        return $this;
616
    }
617
618
    public function getTwitterId()
619
    {
620
        return $this->twitterId;
621
    }
622
623
    public function setTwitterUsername($twitterUsername)
624
    {
625
        $this->twitterUsername = $twitterUsername;
626
627
        return $this;
628
    }
629
630
    public function getTwitterUsername()
631
    {
632
        return $this->twitterUsername;
633
    }
634
635
    public function setTwitterAccessToken($twitterAccessToken)
636
    {
637
        $this->twitterAccessToken = $twitterAccessToken;
638
639
        return $this;
640
    }
641
642
    public function getTwitterAccessToken()
643
    {
644
        return $this->twitterAccessToken;
645
    }
646
647
    public function serialize()
648
    {
649
        return serialize(array($this->facebookId, parent::serialize()));
650
    }
651
652
    public function unserialize($data)
653
    {
654
        list($this->facebookId, $parentData) = unserialize($data);
655
        parent::unserialize($parentData);
656
    }
657
658
    /**
659
     * Get the full name of the user (first + last name)
660
     * @JMS\Groups({"full_name", "name"})
661
     * @JMS\VirtualProperty
662
     * @JMS\SerializedName("full_name")
663
     * @return string
664
     */
665 3
    public function getFullName()
666
    {
667 3
        $fullName = array();
668 3
        if ($this->getFirstname() !== null) {
669 1
            $fullName[] = $this->getFirstname();
670
        }
671 3
        if ($this->getSurname() !== null) {
672 1
            $fullName[] = $this->getSurname();
673
        }
674
675 3
        if (count($fullName) > 0) {
676 1
            return implode(' ', $fullName);
677
        } else {
678 2
            return null;
679
        }
680
    }
681
682
    /**
683
     * Get the full name of the user (first + last name)
684
     * @JMS\Groups({"full_name", "name"})
685
     * @JMS\VirtualProperty
686
     * @JMS\SerializedName("name")
687
     * @return string
688
     */
689
    public function getOIDCName()
690
    {
691
        return $this->getFullName();
692
    }
693
694
    /**
695
     * @JMS\Groups({"badges", "public_profile"})
696
     * @JMS\VirtualProperty
697
     * @JMS\SerializedName("deprecated_badges")
698
     * @JMS\Until("2")
699
     * @return array
700
     */
701
    public function getDataValid()
702
    {
703
        $terms['cpf'] = is_numeric($this->cpf);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$terms was never initialized. Although not strictly required by PHP, it is generally a good practice to add $terms = array(); before regardless.
Loading history...
704
        $terms['email'] = is_null($this->getConfirmationToken());
705
706
        return $terms;
707
    }
708
709
    public function setCpf($cpf)
710
    {
711
        $cpf = trim(preg_replace('/[^0-9]/', '', $cpf));
712
713
        if ($cpf === '') {
714
            $cpf = null;
715
        }
716
717
        $this->cpf = $cpf;
718
719
        return $this;
720
    }
721
722 3
    public function getCpf()
723
    {
724 3
        return $this->cpf;
725
    }
726
727
    /**
728
     * @param \LoginCidadao\CoreBundle\Entity\City $city
729
     * @return City
730
     */
731 1
    public function setCity(\LoginCidadao\CoreBundle\Entity\City $city = null)
732
    {
733 1
        $this->city = $city;
734
735 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LoginCidadao\CoreBundle\Entity\Person which is incompatible with the documented return type LoginCidadao\CoreBundle\Entity\City.
Loading history...
736
    }
737
738
    /**
739
     * @return \LoginCidadao\CoreBundle\Entity\City
740
     */
741 2
    public function getCity()
742
    {
743 2
        return $this->city;
744
    }
745
746 1
    public function setCreatedAt(\DateTime $createdAt)
747
    {
748 1
        $this->createdAt = $createdAt;
749
750 1
        return $this;
751
    }
752
753 1
    public function getCreatedAt()
754
    {
755 1
        return $this->createdAt;
756
    }
757
758
    /**
759
     * @ORM\PrePersist
760
     */
761
    public function setCreatedAtValue()
762
    {
763
        if (!($this->getCreatedAt() instanceof \DateTime)) {
0 ignored issues
show
introduced by
$this->getCreatedAt() is always a sub-type of DateTime.
Loading history...
764
            $this->createdAt = new \DateTime();
765
        }
766
        if (!$this->updatedAt) {
767
            $this->updatedAt = new \DateTime();
768
        }
769
    }
770
771 3
    public function setEmailConfirmedAt(\DateTime $emailConfirmedAt = null)
772
    {
773 3
        $this->emailConfirmedAt = $emailConfirmedAt;
774
775 3
        return $this;
776
    }
777
778 6
    public function getEmailConfirmedAt()
779
    {
780 6
        return $this->emailConfirmedAt;
781
    }
782
783
    public function getSocialNetworksPicture()
784
    {
785
        if (!is_null($this->getFacebookId())) {
0 ignored issues
show
introduced by
The condition is_null($this->getFacebookId()) is always false.
Loading history...
786
            return "https://graph.facebook.com/{$this->getFacebookId()}/picture?height=245&width=245";
787
        }
788
789
        return null;
790
    }
791
792
    public function getClients()
793
    {
794
        return $this->clients;
795
    }
796
797
    public function setClients($var)
798
    {
799
        return $this->clients = $var;
800
    }
801
802
    public function setEmailExpiration($emailExpiration)
803
    {
804
        $this->emailExpiration = $emailExpiration;
805
806
        return $this;
807
    }
808
809
    public function getEmailExpiration()
810
    {
811
        return $this->emailExpiration;
812
    }
813
814
    public function setFacebookUsername($facebookUsername)
815
    {
816
        $this->facebookUsername = $facebookUsername;
817
818
        return $this;
819
    }
820
821
    public function getFacebookUsername()
822
    {
823
        return $this->facebookUsername;
824
    }
825
826
    public function getFacebookAccessToken()
827
    {
828
        return $this->facebookAccessToken;
829
    }
830
831
    public function setFacebookAccessToken($facebookAccessToken)
832
    {
833
        $this->facebookAccessToken = $facebookAccessToken;
834
835
        return $this;
836
    }
837
838
    public function setPreviousValidEmail($previousValidEmail)
839
    {
840
        $this->previousValidEmail = $previousValidEmail;
841
842
        return $this;
843
    }
844
845
    public function getPreviousValidEmail()
846
    {
847
        return $this->previousValidEmail;
848
    }
849
850
    public function hasPassword()
851
    {
852
        $password = $this->getPassword();
853
854
        return strlen($password) > 0;
855
    }
856
857 1
    public function setState(State $state = null)
858
    {
859 1
        $this->state = $state;
860
861 1
        return $this;
862
    }
863
864 2
    public function getState()
865
    {
866 2
        return $this->state;
867
    }
868
869
    /**
870
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
871
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
872
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
873
     * must be able to accept an instance of 'File' as the bundle will inject one here
874
     * during Doctrine hydration.
875
     *
876
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
877
     */
878
    public function setImage($image)
879
    {
880
        $this->image = $image;
881
882
        if ($this->image) {
883
            $this->updatedAt = new \DateTime('now');
884
        }
885
    }
886
887
    /**
888
     * @return File
889
     */
890
    public function getImage()
891
    {
892
        return $this->image;
893
    }
894
895
    /**
896
     * @param string $imageName
897
     */
898
    public function setImageName($imageName)
899
    {
900
        $this->imageName = $imageName;
901
    }
902
903
    /**
904
     * @return string
905
     */
906
    public function getImageName()
907
    {
908
        return $this->imageName;
909
    }
910
911
    public function setProfilePictureUrl($profilePictureUrl)
912
    {
913
        $this->profilePictureUrl = $profilePictureUrl;
914
915
        return $this;
916
    }
917
918
    public function getProfilePictureUrl()
919
    {
920
        return $this->profilePictureUrl;
921
    }
922
923
    /**
924
     * @JMS\Groups({"public_profile"})
925
     * @JMS\VirtualProperty
926
     * @JMS\SerializedName("age_range")
927
     * @JMS\Type("array")
928
     * @return array
929
     */
930
    public function getAgeRange()
931
    {
932
        $today = new \DateTime('today');
933
        if (!$this->getBirthdate()) {
934
            return array();
935
        }
936
        $age = $this->getBirthdate()->diff($today)->y;
937
938
        $range = array();
939
        if ($age < 13) {
940
            $range['max'] = 13;
941
        }
942
        if ($age >= 13 && $age < 18) {
943
            $range['min'] = 13;
944
            $range['max'] = 17;
945
        }
946
        if ($age >= 18 && $age < 21) {
947
            $range['min'] = 18;
948
            $range['max'] = 20;
949
        }
950
        if ($age >= 21) {
951
            $range['min'] = 21;
952
        }
953
954
        return $range;
955
    }
956
957
    public function hasLocalProfilePicture()
958
    {
959
        return !is_null($this->getImageName());
960
    }
961
962
    public function getSuggestions()
963
    {
964
        return $this->suggestions;
965
    }
966
967
    public function setSuggestions($suggestions)
968
    {
969
        $this->suggestions = $suggestions;
970
971
        return $this;
972
    }
973
974
    public function prepareAPISerialize(
975
        $imageHelper,
976
        $templateHelper,
977
        $isDev,
978
        $request
979
    ) {
980
        // User's profile picture
981
        if ($this->hasLocalProfilePicture()) {
982
            $picturePath = $imageHelper->asset($this, 'image');
983
            $pictureUrl = $request->getUriForPath($picturePath);
984
            if ($isDev) {
985
                $pictureUrl = str_replace('/app_dev.php', '', $pictureUrl);
986
            }
987
        } else {
988
            $pictureUrl = $this->getSocialNetworksPicture();
989
        }
990
        if (is_null($pictureUrl)) {
991
            // TODO: fix this and make it comply to DRY
992
            $picturePath = $templateHelper->getUrl('bundles/logincidadaocore/images/userav.png');
993
            $pictureUrl = $request->getUriForPath($picturePath);
994
            if ($isDev) {
995
                $pictureUrl = str_replace('/app_dev.php', '', $pictureUrl);
996
            }
997
        }
998
        $this->setProfilePictureUrl($pictureUrl);
999
        $this->serialize();
1000
    }
1001
1002
    public function isClientAuthorized($app_id)
1003
    {
1004
        foreach ($this->getAuthorizations() as $auth) {
1005
            if ($auth->getClient()->getPublicId() === $app_id) {
1006
                return true;
1007
            }
1008
        }
1009
1010
        return false;
1011
    }
1012
1013
    /**
1014
     * @ORM\PreUpdate
1015
     */
1016 1
    public function setUpdatedAt($updatedAt = null)
1017
    {
1018 1
        if ($updatedAt instanceof \DateTime) {
1019 1
            $this->updatedAt = $updatedAt;
1020
        } else {
1021
            $this->updatedAt = new \DateTime('now');
1022
        }
1023
1024 1
        return $this;
1025
    }
1026
1027 1
    public function getUpdatedAt()
1028
    {
1029 1
        return $this->updatedAt;
1030
    }
1031
1032
    public function setGoogleId($var)
1033
    {
1034
        $this->googleId = $var;
1035
1036
        return $this;
1037
    }
1038
1039
    public function getGoogleId()
1040
    {
1041
        return $this->googleId;
1042
    }
1043
1044
    public function setGoogleUsername($var)
1045
    {
1046
        $this->googleUsername = $var;
1047
1048
        return $this;
1049
    }
1050
1051
    public function getGoogleUsername()
1052
    {
1053
        return $this->googleUsername;
1054
    }
1055
1056
    public function setGoogleAccessToken($var)
1057
    {
1058
        $this->googleAccessToken = $var;
1059
1060
        return $this;
1061
    }
1062
1063
    public function getGoogleAccessToken()
1064
    {
1065
        return $this->googleAccessToken;
1066
    }
1067
1068 1
    public function setCountry(Country $country = null)
1069
    {
1070 1
        $this->country = $country;
1071
1072 1
        return $this;
1073
    }
1074
1075 2
    public function getCountry()
1076
    {
1077 2
        return $this->country;
1078
    }
1079
1080
    public function setComplement($var)
1081
    {
1082
        $this->complement = $var;
0 ignored issues
show
Bug Best Practice introduced by
The property complement does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
1083
1084
        return $this;
1085
    }
1086
1087
    public function getComplement()
1088
    {
1089
        return $this->complement;
1090
    }
1091
1092
    public function getIdCards()
1093
    {
1094
        return $this->idCards;
1095
    }
1096
1097 1
    public function getBadges()
1098
    {
1099 1
        return /** @scrutinizer ignore-deprecated */ $this->badges;
1100
    }
1101
1102 1
    public function mergeBadges(array $badges)
1103
    {
1104 1
        /** @scrutinizer ignore-deprecated */ $this->badges = array_merge(/** @scrutinizer ignore-deprecated */ $this->badges, $badges);
1105
1106 1
        return $this;
1107
    }
1108
1109
    public function getFullNameOrUsername()
1110
    {
1111
        if (null === $this->firstName) {
1112
            return $this->username;
1113
        }
1114
1115
        return $this->getFullName();
1116
    }
1117
1118
    public function getLogoutKeys()
1119
    {
1120
        return $this->logoutKeys;
1121
    }
1122
1123
    /**
1124
     * @return ArrayCollection
1125
     */
1126
    public function getAddresses()
1127
    {
1128
        return $this->addresses;
1129
    }
1130
1131
    public function setLogoutKeys($logoutKeys)
1132
    {
1133
        $this->logoutKeys = $logoutKeys;
1134
1135
        return $this;
1136
    }
1137
1138
    public function setAddresses($addresses)
1139
    {
1140
        $this->addresses = $addresses;
1141
1142
        return $this;
1143
    }
1144
1145
    /**
1146
     * Checks whether 2FA is enabled.
1147
     *
1148
     * @return boolean
1149
     */
1150
    public function isTwoFactorAuthenticationEnabled()
1151
    {
1152
        return $this->googleAuthenticatorSecret !== null;
1153
    }
1154
1155
    public function getGoogleAuthenticatorSecret()
1156
    {
1157
        return $this->googleAuthenticatorSecret;
1158
    }
1159
1160
    public function setGoogleAuthenticatorSecret($googleAuthenticatorSecret)
1161
    {
1162
        $this->googleAuthenticatorSecret = $googleAuthenticatorSecret;
1163
1164
        return $this;
1165
    }
1166
1167
    public function getBackupCodes()
1168
    {
1169
        return $this->backupCodes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->backupCodes returns the type Doctrine\Common\Collections\ArrayCollection which is incompatible with the return type mandated by LoginCidadao\CoreBundle\...rface::getBackupCodes() of LoginCidadao\CoreBundle\Entity\BackupCode[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
1170
    }
1171
1172
    public function setBackupCodes(ArrayCollection $backupCodes)
1173
    {
1174
        $this->backupCodes = $backupCodes;
1175
1176
        return $this;
1177
    }
1178
1179
    public function invalidateBackupCode($code)
1180
    {
1181
        $backupCode = $this->findBackupCode($code);
1182
        $backupCode->setUsed(true);
1183
1184
        return $this;
1185
    }
1186
1187
    public function isBackupCode($code)
1188
    {
1189
        $backupCode = $this->findBackupCode($code);
1190
1191
        return $backupCode !== false && $backupCode->getUsed() === false;
1192
    }
1193
1194
    /**
1195
     * @param string $code
1196
     * @return BackupCode
1197
     */
1198
    private function findBackupCode($code)
1199
    {
1200
        $backupCodes = $this->getBackupCodes();
1201
        foreach ($backupCodes as $backupCode) {
1202
            if ($backupCode->getCode() === $code) {
1203
                return $backupCode;
1204
            }
1205
        }
1206
1207
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type LoginCidadao\CoreBundle\Entity\BackupCode.
Loading history...
1208
    }
1209
1210
    public function setNationality($var)
1211
    {
1212
        $this->nationality = $var;
1213
1214
        return $this;
1215
    }
1216
1217
    public function getNationality()
1218
    {
1219
        return $this->nationality;
1220
    }
1221
1222
    public function getPlaceOfBirth()
1223
    {
1224
        $location = new LocationSelectData();
1225
        $location->getFromObject($this);
1226
1227
        return $location;
1228
    }
1229
1230
    public function setPlaceOfBirth(LocationSelectData $location)
1231
    {
1232
        $location->toObject($this);
1233
    }
1234
1235
    /**
1236
     * @JMS\Groups({"public_profile"})
1237
     * @JMS\VirtualProperty
1238
     * @JMS\SerializedName("given_name")
1239
     * @JMS\Since("1.0")
1240
     */
1241
    public function getGivenName()
1242
    {
1243
        return $this->getFirstName();
1244
    }
1245
1246
    /**
1247
     * @JMS\Groups({"full_name","name"})
1248
     * @JMS\VirtualProperty
1249
     * @JMS\SerializedName("family_name")
1250
     * @JMS\Since("1")
1251
     */
1252
    public function getFamilyName()
1253
    {
1254
        return $this->getSurname();
1255
    }
1256
1257
    /**
1258
     * @JMS\Groups({"mobile", "phone_number"})
1259
     * @JMS\VirtualProperty
1260
     * @JMS\SerializedName("phone_number")
1261
     */
1262
    public function getPhoneNumber()
1263
    {
1264
        return $this->getMobile();
1265
    }
1266
1267
    /**
1268
     * @param bool $verified
1269
     * @return $this
1270
     */
1271 1
    public function setPhoneNumberVerified($verified = false)
1272
    {
1273 1
        $this->phoneNumberVerified = $verified;
1274
1275 1
        return $this;
1276
    }
1277
1278
    /**
1279
     * @return bool
1280
     */
1281 2
    public function getPhoneNumberVerified()
1282
    {
1283 2
        return $this->phoneNumberVerified;
1284
    }
1285
1286
    public function getPasswordEncoderName()
1287
    {
1288
        return $this->passwordEncoderName;
1289
    }
1290
1291
    public function setPasswordEncoderName($passwordEncoderName)
1292
    {
1293
        $this->passwordEncoderName = $passwordEncoderName;
1294
1295
        return $this;
1296
    }
1297
1298
    public function getEncoderName()
1299
    {
1300
        $encoder = $this->passwordEncoderName;
1301
1302
        // BC for PR #357
1303
        if ($encoder === null || strlen($encoder) < 1) {
1304
            return null;
1305
        }
1306
1307
        return $encoder;
1308
    }
1309
1310
    public function getLongDisplayName()
1311
    {
1312
        if ($this->getFullName()) {
1313
            return $this->getFullName();
1314
        } else {
1315
            return $this->getEmail();
1316
        }
1317
    }
1318
1319
    public function getShortDisplayName()
1320
    {
1321
        if ($this->getGivenName()) {
1322
            return $this->getGivenName();
1323
        } else {
1324
            return $this->getEmail();
1325
        }
1326
    }
1327
}
1328