<?php
namespace App\Controller\LicenseRecovery;
use App\Controller\BaseController;
use App\Entity\LicenseRecovery\LicenseRecovery;
use App\Entity\Order\Order;
use App\Entity\VignetteInvoice\VignetteInvoice;
use App\Manager\Cart\CartManager;
use App\Manager\LicenseRecovery\LicenseRecoveryManager;
use App\Manager\Partner\PartnerManager;
use App\Service\AuthTokenService;
use App\Service\InvoiceApiService;
use App\Service\InvoiceService;
use App\Service\SystemService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class LicenseRecoveryController extends BaseController
{
public function indexAction(): Response
{
return $this->render('@templates/Front/LicenseRecovery/license-recovery.html.twig');
}
public function submitData(): Response
{
$licenseRecoveryResponse = $this->getContainer()->get('app.service.license_recovery.license_recovery')
->storeNewLicenseRecoveryInCart(LicenseRecovery::FLAG_SOURCE_PRODUCT_PAGE);
return new Response(json_encode($licenseRecoveryResponse));
}
public function removeFromCart(Request $request): Response
{
$licenseRecoveryService = $this->getContainer()->get('app.service.license_recovery.license_recovery');
$result = $licenseRecoveryService->removeLicenseRecoveryFromCartSessionByCnp($request->get('cnp'));
return new Response(json_encode(['result' => $result]));
}
public function iframe(Request $request): Response
{
$partnerService = $this->getContainer()->get('app.service.partner.partner');
$partnerService->verifyToken($request, PartnerManager::CAN_ISSUE_LICENSE_RECOVERY_INDEX);
$token['partnerPublicToken'] = $request->get(PartnerManager::SESSION_PARTNER_PUBLIC_TOKEN_NAME);
$request->getSession()->set('iframeSourceLink', $this->generateUrl('app_shop_lr_iframe', $token));
$partner = $partnerService->retrievePartner($token['partnerPublicToken']);
if ($partner->getWithoutLocalStorage()) {
CartManager::removeAllProductsFromCartBySession($request->getSession());
}
PartnerManager::checkQRCodeFlow($request);
return $this->render('@templates/Front/LicenseRecovery/iframe.html.twig', [
'guid' => $request->get('guid'),
'publicToken' => $partner->getPublicToken()
]);
}
public function addToCartById(Order $order, Request $request)
{
$clientIp = $request->getClientIp();
$session = $request->getSession();
foreach ($order->getLicenseRecovery() as $licenseRecovery) {
if (!LicenseRecoveryManager::isLicenseRecoveryAlreadyInCart($session)) {
$cartItem = LicenseRecoveryManager::constructLicenseRecoveryForCartFromObject(
$licenseRecovery, $clientIp, LicenseRecovery::FLAG_SOURCE_REMARKETING
);
LicenseRecoveryManager::addNewLicenseRecoveryInCart($session, $cartItem);
}
}
$authTokenService = new AuthTokenService();
if ($token = $authTokenService->authenticateUserWithToken($request, $this->getEntityManager())) {
$this->get('security.token_storage')->setToken($token);
$event = new InteractiveLoginEvent($request, $token);
$this->dispatcher->dispatch($event, 'security.interactive_login');
$session = $request->getSession();
$session->set('_sylius.cart.FASHION_WEB', 180);
$session->save();
}
$utm = [
'utm_source' => $request->get('utm_source'),
'utm_medium' => $request->get('utm_medium'),
'utm_campaign' => $request->get('utm_campaign'),
'utm_term' => $request->get('utm_term'),
'utm_content' => $request->get('utm_content'),
'utm_id' => $request->get('utm_id')
];
return $this->render('@templates/ShopCustom/Redirect/wait_to_be_redirected.html.twig',[
'url' => $this->generateUrl('app_shop_customer_info', $utm)
]);
}
public function showPolicyForPartner(Request $request): Response
{
$id = base64_decode($request->get('order_id'));
$em = $this->getEntityManager();
/** @var Order $order */
$order = $em->getRepository(Order::class)->find($id);
if (!$order->hasLicenseRecovery()) {
return new Response('error');
}
$apiInvoiceService = new InvoiceApiService();
$invoice = $em->getRepository(VignetteInvoice::class)
->findOneBy(['order' => $id, 'productType' => VignetteInvoice::PRODUCT_LICENSE_RECOVERY]);
$pdfContent = $apiInvoiceService->getInvoicePDFViaBillingService($invoice->getApiInvoiceId());
if (!$pdfContent) {
return new Response('error');
}
$invoiceFileName = SystemService::retrieveMessage('invoice', $this->getContainer()->get('translator'));
$filename = "{$invoiceFileName} {$invoice->getSeries()} {$invoice->getNumber()}";
InvoiceService::openPDFContent($pdfContent, $filename);
return new Response();
}
}