Passed
Pull Request — master (#317)
by Guilherme
03:39
created

Blocklist::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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