src/Controller/Pub/CreateUserForMMMAZ.php line 155

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Pub;
  3. use App\DTO\Request\UserPayloadMMAZ;
  4. use App\Entity\Logs\EventLog;
  5. use App\Entity\User\User;
  6. use App\Entity\User\UserProfile;
  7. use App\Event\LogEvent;
  8. use App\Factory\EntityFactory;
  9. use App\Repository\App\CountryRepository;
  10. use App\Repository\App\LanguageRepository;
  11. use App\Repository\User\UserProfileRepository;
  12. use App\Repository\User\UserRepository;
  13. use App\Responders\EntityResponder;
  14. use App\Service\AuthenticationService;
  15. use App\Service\EventLogService;
  16. use App\Service\ProfileService;
  17. use App\Service\RequestDataExtractorService;
  18. use App\Service\UserService;
  19. use Nelmio\ApiDocBundle\Annotation\Model;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  21. use Swagger\Annotations as SWG;
  22. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  23. use Symfony\Component\HttpFoundation\JsonResponse;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. class CreateUserForMMMAZ
  29. {
  30.     /** @var EventDispatcherInterface */
  31.     private $eventDispatcher;
  32.     /**
  33.      * @var RequestDataExtractorService
  34.      */
  35.     private $requestDataExtractorService;
  36.     /**
  37.      * @var EntityFactory
  38.      */
  39.     private $entityFactory;
  40.     /** @var EventLogService */
  41.     private $eventLogService;
  42.     /**
  43.      * @var ProfileService
  44.      */
  45.     private $profileService;
  46.     /**
  47.      * @var AuthenticationService
  48.      */
  49.     private $authenticationService;
  50.     /** @var UserService */
  51.     private $userService;
  52.      /**
  53.       * Create constructor.
  54.       * @param RequestDataExtractorService $requestDataExtractorService
  55.       * @param EntityFactory $entityFactory
  56.       * @param EventDispatcherInterface $eventDispatcher
  57.       * @param EventLogService $eventLogService
  58.       * @param ProfileService $profileService
  59.       * @param AuthenticationService $authenticationService
  60.       * @param UserService $userService
  61.      */
  62.     public function __construct(
  63.         RequestDataExtractorService $requestDataExtractorService,
  64.         EntityFactory $entityFactory,
  65.         EventDispatcherInterface $eventDispatcher,
  66.         EventLogService $eventLogService,
  67.         ProfileService $profileService,
  68.         AuthenticationService $authenticationService,
  69.         UserService $userService
  70.     )
  71.     {
  72.         $this->requestDataExtractorService $requestDataExtractorService;
  73.         $this->entityFactory $entityFactory;
  74.         $this->eventDispatcher $eventDispatcher;
  75.         $this->eventLogService $eventLogService;
  76.         $this->profileService $profileService;
  77.         $this->authenticationService $authenticationService;
  78.         $this->userService $userService;
  79.     }
  80.     /**
  81.      * @Route(path="/user/for-mmaz", methods={"POST"})
  82.      *
  83.      * @SWG\Post(
  84.      *   tags={"Public"}
  85.      * )
  86.      *
  87.      * @SWG\Parameter(
  88.      *     name="body",
  89.      *     in="body",
  90.      *     description="JSON body",
  91.      *     type="json",
  92.      *     @Model(type=UserPayloadMMAZ::class, groups={"user:payloadMMAZ"})
  93.      * )
  94.      *
  95.      * @SWG\Response(
  96.      *     response=200,
  97.      *     description="Success response"
  98.      * )
  99.      *
  100.      * @param Request $request
  101.      * @param EntityResponder $entityResponder
  102.      * @return JsonResponse
  103.      */
  104.     public function __invoke(Request $requestEntityResponder $entityResponder)
  105.     {
  106.         $data $this->requestDataExtractorService->extractDataFromApiRequest([
  107.             'jwt'
  108.         ], $request);
  109.         $data $this->authenticationService->decodeJwt($data['jwt']);
  110.         /*$data = $this->requestDataExtractorService->extractDataFromApiRequest([
  111.             'email',
  112.             'enabled',
  113.             'status',
  114.             //'currency_code',
  115.             //'created_at',
  116.             'two_factor_authentication_enabled',
  117.             //'signed_up_to_mailing_list',
  118.             'user_profile_first_name',
  119.             'user_profile_last_name',
  120.             'user_profile_email_gc',
  121.             'user_profile_country_name',
  122.             //'user_profile_timezone',
  123.             'user_profile_language_code',
  124.         ], $request);*/
  125.         if(!isset($data['email'])){
  126.             throw new BadRequestHttpException('Please fill in required field "email"');
  127.         }
  128.         /** @var User $entityUser */
  129.         $entityUser $this->userService->findUserByEmail($data['email']);
  130.         if(is_null($entityUser)){
  131.             $this->eventLogService->saveDefaultEventLog(
  132.                 $data,
  133.                 EventLog::TYPE_BEFORE_USER_CREATED_MMAZ
  134.             );
  135.            $entityUser $this->userService->createUserFromMMAZ($data);
  136.         }else{
  137.             if((int)$data['status'] === && (int)$data['enabled'] === 0){
  138.                 $this->eventLogService->saveDefaultEventLog(
  139.                     $data,
  140.                     EventLog::TYPE_BEFORE_USER_REVOKED_MMAZ
  141.                 );
  142.                 $this->userService->revokeAccess($entityUser$data);
  143.             }else{
  144.                 $this->eventLogService->saveDefaultEventLog(
  145.                     $data,
  146.                     EventLog::TYPE_BEFORE_USER_UPDATED_MMAZ
  147.                 );
  148.                 $this->userService->updateUserFromMMAZ($entityUser$data);
  149.             }
  150.         }
  151.         /** @var User $user */
  152.         $user $this->userService->findUserByEmail($entityUser->getEmail());
  153.         return $entityResponder($user, ['user:index']);
  154.     }
  155. }