| 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 | 7 |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         UserManager $userManager, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         TwigSwiftMailer $mailer, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         EntityManagerInterface $em, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         PhoneVerificationServiceInterface $phoneVerificationService, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         BlocklistOptions $options | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 7 |  |         $this->userManager = $userManager; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 7 |  |         $this->mailer = $mailer; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 7 |  |         $this->em = $em; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 7 |  |         $this->options = $options; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 7 |  |         $this->blockedPhoneRepository = $this->em->getRepository(BlockedPhoneNumber::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 7 |  |         $this->phoneVerificationService = $phoneVerificationService; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 7 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 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 |  |         $this->em->flush(); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 2 |  |         $blockedUsers = $this->userManager->blockUsersByPhone($phoneNumber, UserManager::FLUSH_STRATEGY_ONCE); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 2 |  |         $this->notifyBlockedUsers($blockedUsers); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 2 |  |         return $blockedUsers; | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 1 |  |     public function checkPhoneNumber(PhoneNumber $phoneNumber): array | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 1 |  |         $blocked = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 1 |  |         if ($this->isPhoneBlocked($phoneNumber)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 1 |  |             $blocked = $this->blockByPhone($phoneNumber); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 1 |  |         return $blocked; | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 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 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 115 | 1 |  |     public function getBlockedPhoneNumberByPhone(PhoneNumber $phoneNumber): ?BlockedPhoneNumberInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         /** @var BlockedPhoneNumberInterface $blockedPhoneNumber */ | 
            
                                                                        
                            
            
                                    
            
            
                | 118 | 1 |  |         $blockedPhoneNumber = $this->blockedPhoneRepository->findByPhone($phoneNumber); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 120 | 1 |  |         return $blockedPhoneNumber; | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |      * @param PersonInterface[] $blockedUsers | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 126 | 2 |  |     private function notifyBlockedUsers(array $blockedUsers) | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 128 | 2 |  |         foreach ($blockedUsers as $person) { | 
            
                                                                        
                            
            
                                    
            
            
                | 129 | 2 |  |             $this->mailer->sendAccountAutoBlockedMessage($person); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 131 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 133 | 4 |  |     private function isManuallyBlocked(PhoneNumber $phoneNumber): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 135 | 4 |  |         return $this->blockedPhoneRepository->findByPhone($phoneNumber) instanceof BlockedPhoneNumberInterface; | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 2 |  |     private function isBlockedAutomatically(PhoneNumber $phoneNumber): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 2 |  |         if ($this->options->isAutoBlockEnabled()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 1 |  |             $autoBlockLimit = $this->options->getAutoBlockPhoneLimit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 1 |  |             return $this->phoneVerificationService->countVerified($phoneNumber) >= $autoBlockLimit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 | 1 |  |         return false; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 148 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 149 |  |  |  |