src/Controller/Api/UserController.php line 98

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: adria
  5.  * Date: 1/5/2019
  6.  * Time: 11:15 AM
  7.  */
  8. namespace App\Controller\Api;
  9. use App\Controller\Response;
  10. use App\Entity\UserPointsHistory;
  11. use App\Entity\Users;
  12. use App\Entity\UserVouchers;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. use DateTime;
  19. use Psr\Log\LoggerInterface;
  20. use App\Services\Subscriptions\AppleSubscriptionService;
  21. use OpenApi\Attributes as OA;
  22. use Nelmio\ApiDocBundle\Annotation\Model;
  23. //use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  24. class UserController extends AbstractController
  25. {
  26.     use Response;
  27.     use \App\Controller\Request;
  28.     protected $translator;
  29.     public function __construct(TranslatorInterface $translator)
  30.     {
  31.         $this->translator $translator;
  32.     }
  33.     private function deleteUser(Users $userEntityManagerInterface $em): void {
  34.         $em->remove($user);
  35.         $em->flush();
  36.     }
  37.     #[Route("/api/user/delete_account/{userId}"
  38.         name"api_delete_user_account"methods: ["POST"])]
  39.     public function deleteAccount($userIdEntityManagerInterface $em)
  40.     {
  41.         $user $this->getUser();   // TOKEN
  42.         if ($user) {
  43.             $this->deleteUser($user$em);
  44.             return $this->json([]);
  45.         } else {
  46.             return $this->errorJsonResponse(['message' => 'User not found']);
  47.         }
  48.     }
  49.     #[Route('/email_verification/{token}/was_not_me'name'api_was_not_me'methods: ['GET'])]
  50.     public function wasNotMeDeleteAccount($tokenRequest $requestEntityManagerInterface $em) {
  51.         $lang $request->get('lang''ro');
  52.         $user $em
  53.             ->getRepository(Users::class)
  54.             ->getByVerificationToken($token);
  55.         if (null === $user) {
  56.             // either token is invalid or association with the user was not found
  57.             return $this->render(
  58.                 'account_creation/wasNotMe'ucfirst($lang) . '.html.twig',
  59.                 ['invalid_token' => true],
  60.             );
  61.         }
  62.         $this->deleteUser($user$em);
  63.         $em->flush();
  64.         return $this->render(
  65.             'account_creation/wasNotMe'ucfirst($lang) . '.html.twig', []
  66.         );
  67.     }
  68.     #[Route("/api/user"name"api_get_current_user"methods: ["GET"]),
  69.         OA\Response(
  70.             response200,
  71.             description"successful user fetch",
  72.             content: new OA\JsonContent(
  73.                 properties: [
  74.                     new OA\Property(property"user"
  75.                         ref: new Model(typeUsers::class))
  76.                 ]
  77.             )
  78.         )
  79.     ]
  80.     public function getCurrentUser(
  81.         EntityManagerInterface $em,
  82.         LoggerInterface $logger,
  83.         AppleSubscriptionService $appleSubscriptionService
  84.     )
  85.     {
  86.         try {
  87.             $user $this->getUser();   // TOKEN
  88.             $user_id $user->getId();
  89.             if (!$user) {
  90.                 throw new \Exception('User not found');
  91.             }
  92.             try {
  93.                 // check subscription renewal
  94.                 $latestSubscription $em->getRepository(UserSubscriptions::class)
  95.                     ->getLastByUser($user_id);
  96.                 if (!$user->hasValidSubscription() && $latestSubscription && $latestSubscription->getPlatform() == 'apple'){
  97.                     $latestTransactionId $latestSubscription->getOriginalTransactionId() ?
  98.                         $latestSubscription->getOriginalTransactionId() :
  99.                         $latestSubscription->getTransactionId();
  100.                     $transaction $appleSubscriptionService->getTransactionInfo($latestTransactionId);
  101.                     $appleSubscriptionService->processTransactionUser($transaction$user);
  102.                 }
  103.             } catch (\Exception $e) {
  104.                 $logger->error(json_encode([
  105.                     'endpoint' => 'api/user',
  106.                     'timestamp' => (new DateTime())->format(DateTime::ATOM),
  107.             'transaction_id' => $transaction->transactionId,
  108.                     'error' => $e->getMessage()
  109.                 ]));
  110.             }
  111.             return $this->jsonResponse([
  112.                 'user' => $em->getRepository(Users::class)->getUser($user->getId()),
  113.             ]);
  114.         } catch (\Exception $e) {
  115.             return $this->errorJsonResponse(['message' => $e->getMessage()]);
  116.         }
  117.     }
  118.     //    /**
  119.     //     * @Route("/api/test_social", name="api_test_social")
  120.     //     */
  121.     //    public function testSocial(ClientRegistry $clientRegistry)
  122.     //    {
  123.     //        /** @var \KnpU\OAuth2ClientBundle\Client\Provider\FacebookClient $client */
  124.     //        $client = $clientRegistry->getClient('facebook_main');
  125.     //
  126.     //        $accessToken = '';
  127.     //
  128.     //        $user = $client->fetchUserFromToken($accessToken);
  129.     //    }
  130. }