src/Service/UserService.php line 181

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\DTO\Request\UserEmailUpdateMMAZ;
  4. use App\DTO\Response\MMAZResponse;
  5. use App\Entity\Notifications\Notification;
  6. use App\Entity\User\User;
  7. use App\Entity\User\UserProfile;
  8. use App\Enums\Constants;
  9. use App\Event\User\UserCreatedEvent;
  10. use App\Event\User\UserRevokeEvent;
  11. use App\Event\User\UserUpdatedEvent;
  12. use App\Repository\App\CountryRepository;
  13. use App\Repository\App\LanguageRepository;
  14. use App\Repository\Notifications\NotificationRepository;
  15. use App\Repository\User\UserProfileRepository;
  16. use App\Repository\User\UserRepository;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Doctrine\ORM\Tools\Pagination\Paginator;
  19. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  20. class UserService
  21. {
  22.     /** @var RequestDataExtractorService */
  23.     private $requestDataExtractorService;
  24.     /** @var EventDispatcherInterface */
  25.     private $eventDispatcher;
  26.     /** @var UserRepository **/
  27.     private $userRepository;
  28.     /** @var UserProfileRepository */
  29.     private $userProfileRepository;
  30.     /** @var CountryRepository */
  31.     private $countryRepository;
  32.     /** @var LanguageRepository */
  33.     private $languageRepository;
  34.     /** @var NotificationRepository */
  35.     private $notificationRepository;
  36.     /**
  37.      * @param RequestDataExtractorService $requestDataExtractorService
  38.      * @param EventDispatcherInterface $eventDispatcher
  39.      * @param UserRepository $userRepository
  40.      * @param UserProfileRepository $userProfileRepository
  41.      * @param CountryRepository $countryRepository
  42.      * @param LanguageRepository $languageRepository
  43.      * @param LanguageRepository $notificationRepository
  44.      */
  45.     public function __construct(
  46.         RequestDataExtractorService $requestDataExtractorService,
  47.         EventDispatcherInterface $eventDispatcher,
  48.         UserRepository $userRepository,
  49.         UserProfileRepository $userProfileRepository,
  50.         CountryRepository $countryRepository,
  51.         LanguageRepository $languageRepository,
  52.         NotificationRepository $notificationRepository
  53.     )
  54.     {
  55.         $this->requestDataExtractorService $requestDataExtractorService;
  56.         $this->eventDispatcher $eventDispatcher;
  57.         $this->userRepository $userRepository;
  58.         $this->userProfileRepository $userProfileRepository;
  59.         $this->countryRepository $countryRepository;
  60.         $this->languageRepository $languageRepository;
  61.         $this->notificationRepository $notificationRepository;
  62.     }
  63.     /**
  64.      * @param string $email
  65.      * @return object|null
  66.      */
  67.     public function findUserByEmail(string $email)
  68.     {
  69.         return $this->userRepository->findOneBy(['email' => $email]);
  70.     }
  71.     /**
  72.      * @param array $data
  73.      * @return User
  74.      */
  75.     public function createUserFromMMAZ(array $data)
  76.     {
  77.         $data $this->checkDataFromMMAZ($data);
  78.         $entityUser = (new User)
  79.             ->setEmail($data['email'])
  80.             ->setEnabled(true)
  81.             ->setTwoFactorAuthenticationEnabled($data['two_factor_authentication_enabled'])
  82.             ->setStatus(Constants::USER_CREATED);
  83.         $this->userRepository->save($entityUser);
  84.         /** @var UserProfile $entityUserProfile */
  85.         $entityUserProfile = (new UserProfile())
  86.             ->setFirstName($data['user_profile_first_name'])
  87.             ->setLastName($data['user_profile_last_name'])
  88.             ->setCountry($this->countryRepository->findOneBy(['country' => $data['user_profile_country_name']]))
  89.             ->setLanguage($this->languageRepository->findOneBy(['code' => $data['user_profile_language_code']]))
  90.             ->setApplications($data['applications'])
  91.             ->setUser($entityUser);
  92.         $this->userProfileRepository->save($entityUserProfile);
  93.         $this->eventDispatcher->dispatch(new UserCreatedEvent($entityUserConstants::MMAZ_REFERER));
  94.         return $entityUser;
  95.     }
  96.     /**
  97.      * @param User $entityUser
  98.      * @param array $data
  99.      * @return User
  100.      */
  101.     public function updateUserFromMMAZ(User $entityUser, array $data)
  102.     {
  103.         $data $this->checkDataFromMMAZ($data);
  104.         $entityUser->setEmail($data['email']);
  105.         $entityUser->setEnabled(true);
  106.         $entityUser->setTwoFactorAuthenticationEnabled($data['two_factor_authentication_enabled']);
  107.         $this->userRepository->flush($entityUser);
  108.         /** @var UserProfile $entityUserProfile */
  109.         $entityUserProfile $entityUser->getUserProfile();
  110.         $entityUserProfile->setFirstName($data['user_profile_first_name']);
  111.         $entityUserProfile->setLastName($data['user_profile_last_name']);
  112.         $entityUserProfile->setCountry($this->countryRepository->findOneBy(['country' => $data['user_profile_country_name']]));
  113.         $entityUserProfile->setLanguage($this->languageRepository->findOneBy(['code' => $data['user_profile_language_code']]));
  114.         $entityUserProfile->setApplications($data['applications']);
  115.         $this->userProfileRepository->flush($entityUserProfile);
  116.         $this->eventDispatcher->dispatch(new UserUpdatedEvent($entityUserConstants::MMAZ_REFERER));
  117.         return $entityUser;
  118.     }
  119.     /**
  120.      * @param UserEmailUpdateMMAZ $emailUpdateMMAZ
  121.      * @return MMAZResponse
  122.      */
  123.     public function updateJustEmailFromMMAZ(UserEmailUpdateMMAZ $emailUpdateMMAZ)
  124.     {
  125.         $response = new MMAZResponse();
  126.         /** @var User $entityUserOld */
  127.         $entityUserOld $this->findUserByEmail($emailUpdateMMAZ->getOldEmail());
  128.         if(is_null($entityUserOld)){
  129.             $response->setStatus("error")
  130.                 ->setMessage("Failed to update email. Reason: this email ({$emailUpdateMMAZ->getOldEmail()}) does not exist");
  131.         }
  132.         /** @var User $entityUserNew */
  133.         $entityUserNew $this->findUserByEmail($emailUpdateMMAZ->getNewEmail());
  134.         if(!is_null($entityUserNew)){
  135.             $response->setStatus("error")
  136.                 ->setMessage("Failed to update email. Reason: this email ({$emailUpdateMMAZ->getNewEmail()}) already exist");
  137.         }
  138.         if($response->getStatus() == "error"){
  139.             return $response;
  140.         }
  141.         $entityUserOld->setEmail($emailUpdateMMAZ->getNewEmail());
  142.         $this->userRepository->flush($entityUserOld);
  143.         $response->setStatus("success")
  144.             ->setMessage("Email updated successfully in MMPZ.");
  145.         return $response;
  146.     }
  147.     /**
  148.      * @param User $entityUser
  149.      * @param array $data
  150.      * @return void
  151.      */
  152.     public function revokeAccess(User $entityUser, array $data)
  153.     {
  154.         $data $this->requestDataExtractorService->extractDataFromAnArray([
  155.             'email',
  156.             'enabled',
  157.             'status',
  158.             'applications'
  159.         ], $data);
  160.         $status = ((int)$data['status'] === 0) ? Constants::USER_SUSPEND Constants::USER_CREATED;
  161.         $entityUser->setStatus($status);
  162.         $entityUser->setEnabled($data['enabled']);
  163.         $this->userRepository->flush($entityUser);
  164.         /** @var UserProfile $entityUserProfile */
  165.         $entityUserProfile $entityUser->getUserProfile();
  166.         $entityUserProfile->setApplications($data['applications']);
  167.         $this->userProfileRepository->flush($entityUserProfile);
  168.         $this->eventDispatcher->dispatch(new UserRevokeEvent($entityUserConstants::MMAZ_REFERER));
  169.     }
  170.     /**
  171.      * @param User $user
  172.      * @param $page
  173.      * @param $limit
  174.      * @return Paginator
  175.      */
  176.     public function notificationInMeEndpoint(User $user$page 1$limit 5): Paginator
  177.     {
  178.         if($_ENV['CWR_ENVIROMENT'] === "local"){
  179.             $user $this->findUserByEmail($_ENV['ADMIN_EMAIL']);
  180.         }
  181.         $queryBuilder $this->notificationRepository->createQueryBuilder('notification')
  182.             ->where('notification.user = :user')
  183.             ->setParameter('user'$user)
  184.             ->orderBy('notification.createdAt''DESC')
  185.             ->setFirstResult(($page 1) * $limit)
  186.             ->setMaxResults($limit);
  187.         $query $queryBuilder->getQuery();
  188.         return new Paginator($query);
  189.     }
  190.     /**
  191.      * @param array $data
  192.      * @return array
  193.      */
  194.     private function checkDataFromMMAZ(array $data)
  195.     {
  196.         $dataChecked $this->requestDataExtractorService->extractDataFromAnArray([
  197.             'email',
  198.             'enabled',
  199.             'status',
  200.             //'currency_code',
  201.             //'created_at',
  202.             'two_factor_authentication_enabled',
  203.             //'signed_up_to_mailing_list',
  204.             'user_profile_first_name',
  205.             'user_profile_last_name',
  206.             'user_profile_email_gc',
  207.             'user_profile_country_name',
  208.             //'user_profile_timezone',
  209.             'user_profile_language_code',
  210.         ], $data);
  211.         return $dataChecked;
  212.     }
  213. }