Failed Conditions
Push — issue#797 ( 813539 )
by Guilherme
07:02
created

AccountRecoveryService::setRecoveryPhone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
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\Service;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use libphonenumber\PhoneNumber;
15
use LoginCidadao\CoreBundle\Entity\AccountRecoveryData;
16
use LoginCidadao\CoreBundle\Entity\AccountRecoveryDataRepository;
17
use LoginCidadao\CoreBundle\Model\PersonInterface;
18
19
class AccountRecoveryService
20
{
21
    /** @var EntityManagerInterface */
22
    private $em;
23
24
    /** @var AccountRecoveryDataRepository */
25
    private $repository;
26
27
    /**
28
     * AccountRecoveryService constructor.
29
     * @param EntityManagerInterface $em
30
     * @param AccountRecoveryDataRepository $repository
31
     */
32
    public function __construct(EntityManagerInterface $em, AccountRecoveryDataRepository $repository)
33
    {
34
        $this->em = $em;
35
        $this->repository = $repository;
36
    }
37
38
    public function getAccountRecoveryData(PersonInterface $person, bool $createIfNotFound = true): ?AccountRecoveryData
39
    {
40
        /** @var AccountRecoveryData $data */
41
        $data = $this->repository->findByPerson($person);
42
        if (null === $data && $createIfNotFound) {
43
            $data = (new AccountRecoveryData())
44
                ->setPerson($person);
45
            $this->em->persist($data);
46
        }
47
48
        return $data;
49
    }
50
51
    public function setRecoveryEmail(PersonInterface $person, string $email): AccountRecoveryData
52
    {
53
        return $this->getAccountRecoveryData($person)
54
            ->setEmail($email);
55
    }
56
57
    public function setRecoveryPhone(PersonInterface $person, PhoneNumber $phoneNumber): AccountRecoveryData
58
    {
59
        return $this->getAccountRecoveryData($person)
60
            ->setMobile($phoneNumber);
61
    }
62
}
63