src/Security/Voter/SubscriberVoter.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Management\Subscription\Formule;
  4. use App\Entity\Management\Subscription\Offer;
  5. use App\Entity\Users\Child;
  6. use App\Repository\Management\SubscriptionRepository;
  7. use App\Service\StripeService;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. class SubscriberVoter extends Voter
  16. {
  17.     public const ACCESS_TUTORAT = 'ACCESS_TUTORAT';
  18.     public const ACCESS_LBP = 'ACCESS_LBP';
  19.     public const ACCESS_FREE = 'ACCESS_FREE';
  20.     public const ACCESS_ONLY_FREE = 'ACCESS_ONLY_FREE';
  21.     public const ACCESS_MISSION = 'ACCESS_MISSION';
  22.     private $subscriptionRepository;
  23.     private $router;
  24.     private $session;
  25.     private $stripe;
  26.     private $entityManager;
  27.     public function __construct(SubscriptionRepository $subscriptionRepository,UrlGeneratorInterface $router,SessionInterface $session,StripeService $stripeService,EntityManagerInterface $entityManager){
  28.         $this->subscriptionRepository = $subscriptionRepository;
  29.         $this->router = $router;
  30.         $this->session = $session;
  31.         $this->stripe = $stripeService;
  32.         $this->entityManager = $entityManager;
  33.     }
  34.     protected function supports(string $attribute, $subject): bool
  35.     {
  36.         // replace with your own logic
  37.         // https://symfony.com/doc/current/security/voters.html
  38.         return in_array($attribute, [self::ACCESS_LBP, self::ACCESS_TUTORAT,self::ACCESS_FREE,self::ACCESS_ONLY_FREE,self::ACCESS_MISSION]);
  39.     }
  40.     protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
  41.     {
  42.         $user = $token->getUser();
  43.         // if the user is anonymous, do not grant access
  44.         if (!$user instanceof Child) {
  45.             return false;
  46.         }
  47.         $last = $this->subscriptionRepository->getLastSubscriptionByChild($user);
  48.         if(!$last){
  49.             return false;
  50.         }
  51.         $now = new \DateTime('now');
  52.         switch ($attribute) {
  53.             case self::ACCESS_FREE:
  54.                 return !in_array("ROLE_FREE",$user->getRoles());
  55.                 break;
  56.             case self::ACCESS_LBP:
  57.                 if(!is_null($last->getUnSubscripted())){
  58.                     if($last->getUnSubscripted()<$now){
  59.                         //$this->session->getFlashBag()->add('notice','Vous n\'avez plus accès à ce service car vous êtes désabonné');
  60.                         return false;
  61.                     }
  62.                 }
  63.                 if($last->isInactif()){
  64.                     if($last->getInactifAt() && $last->getInactifAt()>$now){
  65.                         if($last->getStripeId()){
  66.                             $tmp  = $this->stripe->getStripeClient()->subscriptions->retrieve($last->getStripeId());
  67.                             if($tmp && in_array($tmp->status,['active'])){
  68.                                 $last->setInactif(false);
  69.                                 $last->setInactifAt(null);
  70.                                 $this->entityManager->flush();
  71.                                 return true;
  72.                             }
  73.                         }
  74.                         if(!is_null($last->getPaymentFutur())){
  75.                             $p = $last->getPaymentFutur();
  76.                             if(!is_null($p->getInvoice())){
  77.                                 $inv = $this->stripe->getStripeClient()->invoices->retrieve($p->getInvoice());
  78.                                 if($inv && $inv->paid){
  79.                                     $last->setInactif(false);
  80.                                     $this->entityManager->flush();
  81.                                     return true;
  82.                                 }
  83.                             }
  84.                         }
  85.                     }
  86.                     if($last->getInactifAt() && $last->getInactifAt()<$now){
  87.                         $last->setUnSubscripted($now);
  88.                         $this->entityManager->flush();
  89.                         if($last->getStripeId()){
  90.                             $invoices = $this->stripe->getStripeClient()->invoices->all([
  91.                                 'subscription' => $last->getStripeId(),
  92.                                 'status' => 'open', // Factures en attente de paiement
  93.                             ]);
  94.                             foreach ($invoices->data as $invoice) {
  95.                                 if ($invoice->status === 'open' || $invoice->status === 'draft') {
  96.                                     $this->stripe->getStripeClient()->invoices->markUncollectible($invoice->id);
  97.                                     $this->stripe->getStripeClient()->invoices->voidInvoice($invoice->id);
  98.                                 }
  99.                             }
  100.                             $this->stripe->getStripeClient()->subscriptions->cancel($last->getStripeId(),['invoice_now'=>false]);
  101.                         }
  102.                         return false;
  103.                     }
  104.                     return false;
  105.                 }
  106.                 if(is_null($last->getScriptSubscriptionCheckAt()) || $last->getScriptSubscriptionCheckAt() < $now){
  107.                     if($last->getStripeId() && is_null($last->getUnSubscripted()) && !$last->getIsMobile() && !$last->getB2b() && !$last->IsAccountTest() ){
  108.                         $tmp  = $this->stripe->getStripeClient()->subscriptions->retrieve($last->getStripeId());
  109.                         $date = $last->isTrial()?$last->getTrialDate():$last->getDateUnSubscribed();
  110.                         if($date){
  111.                             $date->modify('+2 days');
  112.                             $last->setScriptSubscriptionCheckAt($date);
  113.                             $this->entityManager->flush();
  114.                         }
  115.                         if(!$tmp || (!in_array($tmp->status,['active','trialing']))){
  116.                             $last->setInactif(true);
  117.                             $expired = (new \DateTime('now'))->setTime(23,59,59,59)->modify("+6 day");
  118.                             $last->setInactifAt($expired);
  119.                             $this->entityManager->flush();
  120.                             return false;
  121.                         }
  122.                     }
  123.                 }
  124.                 return true;
  125.                 // logic to determine if the user can EDIT
  126.                 // return true or false
  127.                 break;
  128.             case self::ACCESS_ONLY_FREE:
  129.                 // logic to determine if the user can VIEW
  130.                 return in_array("ROLE_FREE",$user->getRoles());
  131.                 break;
  132.             case self::ACCESS_MISSION:
  133.                 if($last->getOffer() instanceof Formule && $last->getOffer()->getMonthValid()>=12){
  134.                 }
  135.             break;
  136.         }
  137.         return false;
  138.     }
  139. }