Completed
Pull Request — master (#317)
by Guilherme
03:59
created

Blocklist::addBlockedPhoneNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 9
ccs 5
cts 5
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\PhoneVerificationBundle\Service;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use libphonenumber\PhoneNumber;
15
use LoginCidadao\CoreBundle\Entity\Person;
16
use LoginCidadao\CoreBundle\Entity\PersonRepository;
17
use LoginCidadao\CoreBundle\Mailer\TwigSwiftMailer;
18
use LoginCidadao\CoreBundle\Model\PersonInterface;
19
use LoginCidadao\CoreBundle\Security\User\Manager\UserManager;
20
use LoginCidadao\PhoneVerificationBundle\Entity\BlockedPhoneNumber;
21
use LoginCidadao\PhoneVerificationBundle\Entity\BlockedPhoneNumberRepository;
22
use LoginCidadao\PhoneVerificationBundle\Entity\PhoneVerification;
23
use LoginCidadao\PhoneVerificationBundle\Entity\PhoneVerificationRepository;
24
use LoginCidadao\PhoneVerificationBundle\Model\BlockedPhoneNumberInterface;
25
26
class Blocklist implements BlocklistInterface
27
{
28
    /** @var UserManager */
29
    private $userManager;
30
31
    /** @var TwigSwiftMailer */
32
    private $mailer;
33
34
    /** @var BlockedPhoneNumberRepository */
35
    private $blockedPhoneRepository;
36
37
    /** @var PhoneVerificationServiceInterface */
38
    private $phoneVerificationService;
39
40
    /** @var EntityManagerInterface */
41
    private $em;
42
43
    /** @var BlocklistOptions */
44
    private $options;
45
46
    /**
47
     * Blocklist constructor.
48
     * @param UserManager $userManager
49
     * @param TwigSwiftMailer $mailer
50
     * @param EntityManagerInterface $em
51
     * @param PhoneVerificationServiceInterface $phoneVerificationService
52
     * @param BlocklistOptions $options
53
     */
54 6
    public function __construct(
55
        UserManager $userManager,
56
        TwigSwiftMailer $mailer,
57
        EntityManagerInterface $em,
58
        PhoneVerificationServiceInterface $phoneVerificationService,
59
        BlocklistOptions $options
60
    ) {
61 6
        $this->userManager = $userManager;
62 6
        $this->mailer = $mailer;
63 6
        $this->em = $em;
64 6
        $this->options = $options;
65 6
        $this->blockedPhoneRepository = $this->em->getRepository(BlockedPhoneNumber::class);
66 6
        $this->phoneVerificationService = $phoneVerificationService;
67 6
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72 4
    public function isPhoneBlocked(PhoneNumber $phoneNumber): bool
73
    {
74 4
        return $this->isManuallyBlocked($phoneNumber) || $this->isBlockedAutomatically($phoneNumber);
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80 2
    public function blockByPhone(PhoneNumber $phoneNumber): array
81
    {
82 2
        $blockedUsers = $this->userManager->blockUsersByPhone($phoneNumber, UserManager::FLUSH_STRATEGY_ONCE);
83 2
        $this->notifyBlockedUsers($blockedUsers);
84
85 2
        return $blockedUsers;
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91 1
    public function checkPhoneNumber(PhoneNumber $phoneNumber): array
92
    {
93 1
        $blocked = [];
94 1
        if ($this->isPhoneBlocked($phoneNumber)) {
95 1
            $blocked = $this->blockByPhone($phoneNumber);
96
        }
97
98 1
        return $blocked;
99
    }
100
101 1
    public function addBlockedPhoneNumber(
102
        PhoneNumber $phoneNumber,
103
        PersonInterface $blockedBy
104
    ): BlockedPhoneNumberInterface {
105 1
        $blockedPhoneNumber = new BlockedPhoneNumber($phoneNumber, $blockedBy, new \DateTime());
106 1
        $this->em->persist($blockedPhoneNumber);
107 1
        $this->em->flush();
108
109 1
        return $blockedPhoneNumber;
110
    }
111
112
    /**
113
     * @param PersonInterface[] $blockedUsers
114
     */
115 2
    private function notifyBlockedUsers(array $blockedUsers)
116
    {
117 2
        foreach ($blockedUsers as $person) {
118 2
            $this->mailer->sendAccountAutoBlockedMessage($person);
119
        }
120 2
    }
121
122 4
    private function isManuallyBlocked(PhoneNumber $phoneNumber): bool
123
    {
124 4
        return $this->blockedPhoneRepository->findByPhone($phoneNumber) instanceof BlockedPhoneNumberInterface;
125
    }
126
127 2
    private function isBlockedAutomatically(PhoneNumber $phoneNumber): bool
128
    {
129 2
        if ($this->options->isAutoBlockEnabled()) {
130 1
            $autoBlockLimit = $this->options->getAutoBlockPhoneLimit();
131
132 1
            return $this->phoneVerificationService->countVerified($phoneNumber) >= $autoBlockLimit;
133
        }
134
135 1
        return false;
136
    }
137
}
138