<?php
/**
* Created by PhpStorm.
* User: adria
* Date: 1/5/2019
* Time: 11:15 AM
*/
namespace App\Controller\Api;
use App\Controller\Response;
use App\Entity\UserPointsHistory;
use App\Entity\Users;
use App\Entity\UserVouchers;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use DateTime;
use Psr\Log\LoggerInterface;
use App\Services\Subscriptions\AppleSubscriptionService;
use OpenApi\Attributes as OA;
use Nelmio\ApiDocBundle\Annotation\Model;
//use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
class UserController extends AbstractController
{
use Response;
use \App\Controller\Request;
protected $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
private function deleteUser(Users $user, EntityManagerInterface $em): void {
$em->remove($user);
$em->flush();
}
#[Route("/api/user/delete_account/{userId}",
name: "api_delete_user_account", methods: ["POST"])]
public function deleteAccount($userId, EntityManagerInterface $em)
{
$user = $this->getUser(); // TOKEN
if ($user) {
$this->deleteUser($user, $em);
return $this->json([]);
} else {
return $this->errorJsonResponse(['message' => 'User not found']);
}
}
#[Route('/email_verification/{token}/was_not_me', name: 'api_was_not_me', methods: ['GET'])]
public function wasNotMeDeleteAccount($token, Request $request, EntityManagerInterface $em) {
$lang = $request->get('lang', 'ro');
$user = $em
->getRepository(Users::class)
->getByVerificationToken($token);
if (null === $user) {
// either token is invalid or association with the user was not found
return $this->render(
'account_creation/wasNotMe'. ucfirst($lang) . '.html.twig',
['invalid_token' => true],
);
}
$this->deleteUser($user, $em);
$em->flush();
return $this->render(
'account_creation/wasNotMe'. ucfirst($lang) . '.html.twig', []
);
}
#[Route("/api/user", name: "api_get_current_user", methods: ["GET"]),
OA\Response(
response: 200,
description: "successful user fetch",
content: new OA\JsonContent(
properties: [
new OA\Property(property: "user",
ref: new Model(type: Users::class))
]
)
)
]
public function getCurrentUser(
EntityManagerInterface $em,
LoggerInterface $logger,
AppleSubscriptionService $appleSubscriptionService
)
{
try {
$user = $this->getUser(); // TOKEN
$user_id = $user->getId();
if (!$user) {
throw new \Exception('User not found');
}
try {
// check subscription renewal
$latestSubscription = $em->getRepository(UserSubscriptions::class)
->getLastByUser($user_id);
if (!$user->hasValidSubscription() && $latestSubscription && $latestSubscription->getPlatform() == 'apple'){
$latestTransactionId = $latestSubscription->getOriginalTransactionId() ?
$latestSubscription->getOriginalTransactionId() :
$latestSubscription->getTransactionId();
$transaction = $appleSubscriptionService->getTransactionInfo($latestTransactionId);
$appleSubscriptionService->processTransactionUser($transaction, $user);
}
} catch (\Exception $e) {
$logger->error(json_encode([
'endpoint' => 'api/user',
'timestamp' => (new DateTime())->format(DateTime::ATOM),
'transaction_id' => $transaction->transactionId,
'error' => $e->getMessage()
]));
}
return $this->jsonResponse([
'user' => $em->getRepository(Users::class)->getUser($user->getId()),
]);
} catch (\Exception $e) {
return $this->errorJsonResponse(['message' => $e->getMessage()]);
}
}
// /**
// * @Route("/api/test_social", name="api_test_social")
// */
// public function testSocial(ClientRegistry $clientRegistry)
// {
// /** @var \KnpU\OAuth2ClientBundle\Client\Provider\FacebookClient $client */
// $client = $clientRegistry->getClient('facebook_main');
//
// $accessToken = '';
//
// $user = $client->fetchUserFromToken($accessToken);
// }
}