<?php
namespace App\Security\Voter;
use App\Entity\Users\Child;
use App\Repository\Management\SubscriptionRepository;
use App\Service\StripeService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class SubscriberVoter extends Voter
{
public const ACCESS_TUTORAT = 'ACCESS_TUTORAT';
public const ACCESS_LBP = 'ACCESS_LBP';
public const ACCESS_FREE = 'ACCESS_FREE';
public const ACCESS_ONLY_FREE = 'ACCESS_ONLY_FREE';
private $subscriptionRepository;
private $router;
private $session;
private $stripe;
private $entityManager;
public function __construct(SubscriptionRepository $subscriptionRepository,UrlGeneratorInterface $router,SessionInterface $session,StripeService $stripeService,EntityManagerInterface $entityManager){
$this->subscriptionRepository = $subscriptionRepository;
$this->router = $router;
$this->session = $session;
$this->stripe = $stripeService;
$this->entityManager = $entityManager;
}
protected function supports(string $attribute, $subject): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, [self::ACCESS_LBP, self::ACCESS_TUTORAT,self::ACCESS_FREE,self::ACCESS_ONLY_FREE]);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof Child) {
return false;
}
$last = $this->subscriptionRepository->getLastSubscriptionByChild($user);
if(!$last){
return false;
}
$now = new \DateTime('now');
switch ($attribute) {
case self::ACCESS_FREE:
return !in_array("ROLE_FREE",$user->getRoles());
break;
case self::ACCESS_LBP:
if(!is_null($last->getUnSubscripted())){
if($last->getUnSubscripted()<$now){
//$this->session->getFlashBag()->add('notice','Vous n\'avez plus accès à ce service car vous êtes désabonné');
return false;
}
}
if($last->isInactif()){
if($last->getInactifAt() && $last->getInactifAt()>$now){
if($last->getStripeId()){
$tmp = $this->stripe->getStripeClient()->subscriptions->retrieve($last->getStripeId());
if($tmp && in_array($tmp->status,['active'])){
$last->setInactif(false);
$this->entityManager->flush();
return true;
}
}
if(!is_null($last->getPaymentFutur())){
$p = $last->getPaymentFutur();
if(!is_null($p->getInvoice())){
$inv = $this->stripe->getStripeClient()->invoices->retrieve($p->getInvoice());
if($inv && $inv->paid){
$last->setInactif(false);
$this->entityManager->flush();
return true;
}
}
}
}
if($last->getInactifAt() && $last->getInactifAt()<$now){
$last->setUnSubscripted($now);
$this->entityManager->flush();
if($last->getStripeId()){
$this->stripe->getStripeClient()->subscriptions->cancel($last->getStripeId(),['invoice_now'=>false]);
}
return false;
}
return false;
}
if(is_null($last->getScriptSubscriptionCheckAt()) || $last->getScriptSubscriptionCheckAt() < $now){
if($last->getStripeId() && is_null($last->getUnSubscripted()) && !$last->getIsMobile() && !$last->getB2b() && !$last->IsAccountTest() ){
$tmp = $this->stripe->getStripeClient()->subscriptions->retrieve($last->getStripeId());
$date = $last->isTrial()?$last->getTrialDate():$last->getDateUnSubscribed();
if($date){
$date->modify('+2 days');
$last->setScriptSubscriptionCheckAt($date);
$this->entityManager->flush();
}
if(!$tmp || (!in_array($tmp->status,['active','trialing']))){
$last->setInactif(true);
$expired = (new \DateTime('now'))->setTime(23,59,59,59)->modify("+6 day");
$last->setInactifAt($expired);
$this->entityManager->flush();
return false;
}
}
}
return true;
// logic to determine if the user can EDIT
// return true or false
break;
case self::ACCESS_ONLY_FREE:
// logic to determine if the user can VIEW
return in_array("ROLE_FREE",$user->getRoles());
break;
}
return false;
}
}