Passed
Push — master ( 1d2904...e25fdf )
by Guilherme
01:18 queued 12s
created

SupportPerson::getThirdPartyConnections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\Model\IdentifiablePersonInterface;
17
use LoginCidadao\CoreBundle\Model\PersonInterface;
18
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
19
20
class SupportPerson implements IdentifiablePersonInterface
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 bool */
53
    private $isEnabled;
54
55
    /** @var \DateTimeInterface */
56
    private $lastUpdate;
57
58
    /** @var \DateTimeInterface */
59
    private $createdAt;
60
61
    /** @var \DateTimeInterface */
62
    private $lastLogin;
63
64
    /**
65
     * SupportPerson constructor.
66
     * @param PersonInterface $person
67
     * @param AuthorizationCheckerInterface $authorizationChecker
68
     */
69 4
    public function __construct(PersonInterface $person, AuthorizationCheckerInterface $authorizationChecker)
70
    {
71 4
        $this->id = $person->getId();
72 4
        $this->firstName = $person->getFirstName();
73 4
        $this->lastName = $person->getSurname();
74 4
        $this->emailVerifiedAt = $person->getEmailConfirmedAt();
75 4
        $this->lastPasswordResetRequest = $person->getPasswordRequestedAt();
76 4
        $this->has2FA = $person->getGoogleAuthenticatorSecret() !== null;
77 4
        $this->isEnabled = $person->isEnabled();
78 4
        $this->lastUpdate = $person->getUpdatedAt();
79 4
        $this->createdAt = $person->getCreatedAt();
80 4
        $this->lastLogin = $person->getLastLogin();
81
82 4
        if ($authorizationChecker->isGranted('ROLE_VIEW_USERS_CPF')) {
83 1
            $this->cpf = PersonalData::createWithValue('cpf', $person->getCpf());
84
        } else {
85 3
            $this->cpf = PersonalData::createWithoutValue('cpf', $person->getCpf());
86
        }
87 4
        if ($authorizationChecker->isGranted('ROLE_SUPPORT_VIEW_EMAIL')) {
88 1
            $this->email = PersonalData::createWithValue('email', $person->getEmailCanonical());
89
        } else {
90 3
            $this->email = PersonalData::createWithoutValue('email', $person->getEmailCanonical());
91
        }
92 4
        $this->setPhoneNumber($person, $authorizationChecker);
93 4
        $this->setBirthday($person, $authorizationChecker);
94 4
    }
95
96 4
    private function setPhoneNumber(PersonInterface $person, AuthorizationCheckerInterface $authorizationChecker)
97
    {
98 4
        $phoneNumber = $person->getMobile();
99 4
        if ($phoneNumber instanceof PhoneNumber) {
100 2
            $phoneUtil = PhoneNumberUtil::getInstance();
101 2
            $phoneNumber = $phoneUtil->format($phoneNumber, PhoneNumberFormat::E164);
102
        }
103 4
        if ($authorizationChecker->isGranted('ROLE_SUPPORT_VIEW_PHONE')) {
104 1
            $this->phoneNumber = PersonalData::createWithValue('phoneNumber', $phoneNumber);
105
        } else {
106 3
            $this->phoneNumber = PersonalData::createWithoutValue('phoneNumber', $phoneNumber);
107
        }
108 4
    }
109
110 4
    private function setBirthday(PersonInterface $person, AuthorizationCheckerInterface $authorizationChecker)
111
    {
112 4
        $birthday = $person->getBirthdate();
113 4
        if ($birthday instanceof \DateTimeInterface) {
0 ignored issues
show
introduced by
$birthday is always a sub-type of DateTimeInterface.
Loading history...
114 2
            $birthday = $birthday->format('Y-m-d');
115
        }
116
117 4
        if ($authorizationChecker->isGranted('ROLE_SUPPORT_VIEW_BIRTHDAY')) {
118 1
            $this->birthday = PersonalData::createWithValue('birthday', $birthday);
119
        } else {
120 3
            $this->birthday = PersonalData::createWithoutValue('birthday', $birthday);
121
        }
122 4
    }
123
124 1
    public function getName(): string
125
    {
126 1
        return $this->getFirstName() ?? $this->getId();
127
    }
128
129 3
    public function getId()
130
    {
131 3
        return $this->id;
132
    }
133
134
    /**
135
     * @return string
136
     */
137 3
    public function getFirstName(): ?string
138
    {
139 3
        return $this->firstName;
140
    }
141
142
    /**
143
     * @return string
144
     */
145 2
    public function getLastName(): ?string
146
    {
147 2
        return $this->lastName;
148
    }
149
150
    /**
151
     * @return PersonalData
152
     */
153 2
    public function getBirthday(): PersonalData
154
    {
155 2
        return $this->birthday;
156
    }
157
158
    /**
159
     * @return PersonalData
160
     */
161 2
    public function getCpf(): PersonalData
162
    {
163 2
        return $this->cpf;
164
    }
165
166
    /**
167
     * @return PersonalData
168
     */
169 2
    public function getEmail(): PersonalData
170
    {
171 2
        return $this->email;
172
    }
173
174
    /**
175
     * @return \DateTimeInterface
176
     */
177 2
    public function getEmailVerifiedAt(): ?\DateTimeInterface
178
    {
179 2
        return $this->emailVerifiedAt;
180
    }
181
182
    /**
183
     * @return PersonalData
184
     */
185 2
    public function getPhoneNumber(): PersonalData
186
    {
187 2
        return $this->phoneNumber;
188
    }
189
190
    /**
191
     * @return \DateTimeInterface
192
     */
193 2
    public function getLastPasswordResetRequest(): ?\DateTimeInterface
194
    {
195 2
        return $this->lastPasswordResetRequest;
196
    }
197
198
    /**
199
     * @return bool
200
     */
201 2
    public function has2FA(): bool
202
    {
203 2
        return $this->has2FA;
204
    }
205
206
    /**
207
     * @return bool
208
     */
209 2
    public function isEnabled(): bool
210
    {
211 2
        return $this->isEnabled;
212
    }
213
214
    /**
215
     * @return \DateTimeInterface
216
     */
217 2
    public function getLastUpdate(): \DateTimeInterface
218
    {
219 2
        return $this->lastUpdate;
220
    }
221
222
    /**
223
     * @return \DateTimeInterface
224
     */
225 2
    public function getCreatedAt(): \DateTimeInterface
226
    {
227 2
        return $this->createdAt;
228
    }
229
230
    /**
231
     * @return \DateTimeInterface
232
     */
233 1
    public function getLastLogin(): \DateTimeInterface
234
    {
235 1
        return $this->lastLogin;
236
    }
237
}
238