| 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\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use libphonenumber\PhoneNumber; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use LoginCidadao\CoreBundle\Entity\PersonRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use LoginCidadao\CoreBundle\Entity\SentEmail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use LoginCidadao\CoreBundle\Entity\SentEmailRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use LoginCidadao\CoreBundle\Model\IdentifiablePersonInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use LoginCidadao\CoreBundle\Model\PersonInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use LoginCidadao\PhoneVerificationBundle\Service\PhoneVerificationServiceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use LoginCidadao\SupportBundle\Exception\PersonNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use LoginCidadao\SupportBundle\Model\PersonalData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use LoginCidadao\SupportBundle\Model\SupportPerson; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | class SupportHandler | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** @var AuthorizationCheckerInterface */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     private $authChecker; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** @var PhoneVerificationServiceInterface */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     private $phoneVerificationService; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     /** @var PersonRepository */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     private $personRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     /** @var SentEmailRepository */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     private $sentEmailRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * SupportHandler constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @param AuthorizationCheckerInterface $authChecker | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * @param PhoneVerificationServiceInterface $phoneVerificationService | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param PersonRepository $personRepository | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param SentEmailRepository $sentEmailRepository | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 10 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         AuthorizationCheckerInterface $authChecker, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         PhoneVerificationServiceInterface $phoneVerificationService, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         PersonRepository $personRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         SentEmailRepository $sentEmailRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 10 |  |         $this->authChecker = $authChecker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 10 |  |         $this->phoneVerificationService = $phoneVerificationService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 10 |  |         $this->personRepository = $personRepository; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 | 10 |  |         $this->sentEmailRepository = $sentEmailRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 10 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |     public function getSupportPerson($id): SupportPerson | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |         $person = $this->personRepository->find($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 2 |  |         if (!$person instanceof PersonInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |             throw new PersonNotFoundException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 | 1 |  |         return new SupportPerson($person, $this->authChecker); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 3 |  |     public function getThirdPartyConnections(IdentifiablePersonInterface $person): array | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 3 |  |         $connections = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 3 |  |         if (!$person instanceof PersonInterface) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             /** @var PersonInterface $person */ | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 2 |  |             $person = $this->personRepository->find($person->getId()); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 3 |  |         if ($person instanceof PersonInterface) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             $connections = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 2 |  |                 'facebook' => $person->getFacebookId() !== null, | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 2 |  |                 'google' => $person->getGoogleId() !== null, | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 2 |  |                 'twitter' => $person->getTwitterId() !== null, | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 3 |  |         return $connections; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 3 |  |     public function getPhoneMetadata(IdentifiablePersonInterface $person): array | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         /** @var PersonInterface $person */ | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 3 |  |         $person = $this->personRepository->find($person->getId()); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 3 |  |         $phone = $person->getMobile(); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 3 |  |         if ($phone instanceof PhoneNumber) { | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 2 |  |             $samePhoneCount = $this->personRepository->countByPhone($phone); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 2 |  |             $phoneVerification = $this->phoneVerificationService->getPhoneVerification($person, $phone); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 3 |  |             'samePhoneCount' => $samePhoneCount ?? 0, | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 3 |  |             'verification' => $phoneVerification ?? null, | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |     public function getInitialMessage($ticket): ?SentEmail | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         /** @var SentEmail $sentEmail */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |         $sentEmail = $this->sentEmailRepository->findOneBy(['supportTicket' => $ticket]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 | 1 |  |         return $sentEmail; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |     public function getValidationMap(SupportPerson $person): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |         return array_filter([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |             'cpf' => $this->personalDataToValidationArray($person->getCpf(), true), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |             'birthday' => $this->personalDataToValidationArray($person->getBirthday(), true), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |             'email' => $this->personalDataToValidationArray($person->getEmail(), true), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 | 1 |  |             'phoneNumber' => $this->personalDataToValidationArray($person->getPhoneNumber(), true), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |     private function personalDataToValidationArray(PersonalData $data, bool $skipIfValueSet = false): ?array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |         if (false === $data->isValueFilled()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |         if ($skipIfValueSet && $data->getValue() !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |             'name' => $data->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |             'hash' => $data->getHash(), | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 134 | 1 |  |             'challenge' => $data->getChallenge(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 137 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |  |