Failed Conditions
Push — issue#699 ( 7155bc )
by Guilherme
07:26
created

SupportPerson::setBirthday()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 11
ccs 0
cts 10
cp 0
crap 12
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\SupportBundle\Model;
12
13
use libphonenumber\PhoneNumber;
14
use libphonenumber\PhoneNumberFormat;
15
use libphonenumber\PhoneNumberUtil;
16
use LoginCidadao\CoreBundle\Entity\Person;
17
use LoginCidadao\CoreBundle\Model\PersonInterface;
18
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
19
20
class SupportPerson
21
{
22
    /** @var mixed */
23
    private $id;
24
25
    /** @var string */
26
    private $firstName;
27
28
    /** @var string */
29
    private $lastName;
30
31
    /** @var PersonalData */
32
    private $birthday;
33
34
    /** @var PersonalData */
35
    private $cpf;
36
37
    /** @var PersonalData */
38
    private $email;
39
40
    /** @var \DateTimeInterface */
41
    private $emailVerifiedAt;
42
43
    /** @var PersonalData */
44
    private $phoneNumber;
45
46
    /** @var \DateTimeInterface */
47
    private $lastPasswordResetRequest;
48
49
    /** @var bool */
50
    private $has2FA;
51
52
    /** @var array */
53
    private $thirdPartyConnections = [];
54
55
    /** @var bool */
56
    private $isEnabled;
57
58
    /** @var \DateTimeInterface */
59
    private $lastUpdate;
60
61
    /** @var \DateTimeInterface */
62
    private $createdAt;
63
64
    /**
65
     * SupportPerson constructor.
66
     * @param PersonInterface|Person $person
67
     * @param AuthorizationCheckerInterface $authorizationChecker
68
     */
69
    public function __construct(PersonInterface $person, AuthorizationCheckerInterface $authorizationChecker)
70
    {
71
        $this->id = $person->getId();
72
        $this->firstName = $person->getFirstName();
73
        $this->lastName = $person->getSurname();
74
        $this->emailVerifiedAt = $person->getEmailConfirmedAt();
75
        $this->lastPasswordResetRequest = $person->getPasswordRequestedAt();
0 ignored issues
show
Bug introduced by
The method getPasswordRequestedAt() does not exist on LoginCidadao\CoreBundle\Model\PersonInterface. Did you maybe mean getPassword()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        /** @scrutinizer ignore-call */ 
76
        $this->lastPasswordResetRequest = $person->getPasswordRequestedAt();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
        $this->has2FA = $person->getGoogleAuthenticatorSecret() !== null;
77
        $this->isEnabled = $person->isEnabled();
78
        $this->lastUpdate = $person->getUpdatedAt();
79
        $this->createdAt = $person->getCreatedAt();
80
81
        $this->thirdPartyConnections = [
82
            'facebook' => $person->getFacebookId() !== null,
83
            'google' => $person->getGoogleId() !== null,
84
            'twitter' => $person->getTwitterId() !== null,
85
        ];
86
87
        if ($authorizationChecker->isGranted('ROLE_VIEW_USERS_CPF')) {
88
            $this->cpf = PersonalData::createWithValue('cpf', $person->getCpf());
89
        } else {
90
            $this->cpf = PersonalData::createWithoutValue('cpf', $person->getCpf());
91
        }
92
        if ($authorizationChecker->isGranted('ROLE_SUPPORT_VIEW_EMAIL')) {
93
            $this->email = PersonalData::createWithValue('email', $person->getEmailCanonical());
94
        } else {
95
            $this->email = PersonalData::createWithoutValue('email', $person->getEmailCanonical());
96
        }
97
        $this->setPhoneNumber($person, $authorizationChecker);
98
        $this->setBirthday($person, $authorizationChecker);
99
    }
100
101
    public function checkCpf(string $cpf)
102
    {
103
        return $this->cpf->checkValue($cpf);
104
    }
105
106
    private function setPhoneNumber(PersonInterface $person, AuthorizationCheckerInterface $authorizationChecker)
107
    {
108
        $phoneNumber = $person->getMobile();
109
        if ($phoneNumber instanceof PhoneNumber) {
110
            $phoneUtil = PhoneNumberUtil::getInstance();
111
            $phoneNumber = $phoneUtil->format($person->getMobile(), PhoneNumberFormat::E164);
112
        }
113
        if ($authorizationChecker->isGranted('ROLE_SUPPORT_VIEW_PHONE')) {
114
            $this->phoneNumber = PersonalData::createWithValue('phoneNumber', $phoneNumber);
115
        } else {
116
            $this->phoneNumber = PersonalData::createWithoutValue('phoneNumber', $phoneNumber);
117
        }
118
    }
119
120
    private function setBirthday(PersonInterface $person, AuthorizationCheckerInterface $authorizationChecker)
121
    {
122
        $birthday = $person->getBirthdate();
123
        if ($birthday instanceof \DateTimeInterface) {
0 ignored issues
show
introduced by
$birthday is always a sub-type of DateTimeInterface.
Loading history...
124
            $birthday = $birthday->format('Y-m-d');
125
        }
126
127
        if ($authorizationChecker->isGranted('ROLE_SUPPORT_VIEW_BIRTHDAY')) {
128
            $this->birthday = PersonalData::createWithValue('birthday', $birthday);
129
        } else {
130
            $this->birthday = PersonalData::createWithoutValue('birthday', $birthday);
131
        }
132
    }
133
134
    public function getName(): string
135
    {
136
        return $this->getFirstName() ?? $this->getId();
137
    }
138
139
    public function getId()
140
    {
141
        return $this->id;
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    public function getFirstName(): ?string
148
    {
149
        return $this->firstName;
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getLastName(): ?string
156
    {
157
        return $this->lastName;
158
    }
159
160
    /**
161
     * @return PersonalData
162
     */
163
    public function getBirthday(): PersonalData
164
    {
165
        return $this->birthday;
166
    }
167
168
    /**
169
     * @return PersonalData
170
     */
171
    public function getCpf(): PersonalData
172
    {
173
        return $this->cpf;
174
    }
175
176
    /**
177
     * @return PersonalData
178
     */
179
    public function getEmail(): PersonalData
180
    {
181
        return $this->email;
182
    }
183
184
    /**
185
     * @return \DateTimeInterface
186
     */
187
    public function getEmailVerifiedAt(): ?\DateTimeInterface
188
    {
189
        return $this->emailVerifiedAt;
190
    }
191
192
    /**
193
     * @return PersonalData
194
     */
195
    public function getPhoneNumber(): PersonalData
196
    {
197
        return $this->phoneNumber;
198
    }
199
200
    /**
201
     * @return \DateTimeInterface
202
     */
203
    public function getLastPasswordResetRequest(): ?\DateTimeInterface
204
    {
205
        return $this->lastPasswordResetRequest;
206
    }
207
208
    /**
209
     * @return bool
210
     */
211
    public function isHas2FA(): bool
212
    {
213
        return $this->has2FA;
214
    }
215
216
    /**
217
     * @return array
218
     */
219
    public function getThirdPartyConnections(): array
220
    {
221
        return $this->thirdPartyConnections;
222
    }
223
224
    /**
225
     * @return bool
226
     */
227
    public function isEnabled(): bool
228
    {
229
        return $this->isEnabled;
230
    }
231
232
    /**
233
     * @return \DateTimeInterface
234
     */
235
    public function getLastUpdate(): \DateTimeInterface
236
    {
237
        return $this->lastUpdate;
238
    }
239
240
    /**
241
     * @return \DateTimeInterface
242
     */
243
    public function getCreatedAt(): \DateTimeInterface
244
    {
245
        return $this->createdAt;
246
    }
247
}
248