vendor/bluue/gocardless-bundle/src/EventSubscriber/SalesBundle/DocumentTabsEventSubscriber.php line 92

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Quentin CHATELAIN (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\GocardlessBundle\EventSubscriber\SalesBundle;
  9. use Bluue\GocardlessBundle\Repository\CustomerMappingRepository;
  10. use Bluue\GocardlessBundle\Repository\DirectDebitRepository;
  11. use Bluue\SalesBundle\Event\DocumentTabsEvent;
  12. use Bluue\SalesBundle\Repository\InvoiceRepository;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class DocumentTabsEventSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var TranslatorInterface
  21.      */
  22.     private TranslatorInterface $tr;
  23.     /**
  24.      * @var RequestStack
  25.      */
  26.     private RequestStack $requestStack;
  27.     /**
  28.      * @var CustomerMappingRepository
  29.      */
  30.     private CustomerMappingRepository $customerMappingRepo;
  31.     /**
  32.      * @var InvoiceRepository
  33.      */
  34.     private InvoiceRepository $invoiceRepo;
  35.     /**
  36.      * @var DirectDebitRepository
  37.      */
  38.     private DirectDebitRepository $directDebitRepo;
  39.     /**
  40.      * @var Security
  41.      */
  42.     private Security $security;
  43.     /**
  44.      * @param TranslatorInterface $tr
  45.      * @param RequestStack $requestStack
  46.      * @param CustomerMappingRepository $customerMappingRepo
  47.      * @param InvoiceRepository $invoiceRepo
  48.      * @param DirectDebitRepository $directDebitRepo
  49.      * @param Security $security
  50.      */
  51.     public function __construct(
  52.         TranslatorInterface $tr,
  53.         RequestStack $requestStack,
  54.         CustomerMappingRepository $customerMappingRepo,
  55.         InvoiceRepository $invoiceRepo,
  56.         DirectDebitRepository $directDebitRepo,
  57.         Security $security
  58.     ) {
  59.         $this->tr $tr;
  60.         $this->requestStack $requestStack;
  61.         $this->customerMappingRepo $customerMappingRepo;
  62.         $this->invoiceRepo $invoiceRepo;
  63.         $this->directDebitRepo $directDebitRepo;
  64.         $this->security $security;
  65.     }
  66.     /**
  67.      * @return string[]
  68.      */
  69.     public static function getSubscribedEvents(): array
  70.     {
  71.         return [
  72.             DocumentTabsEvent::INVOICE_TABS => 'invoiceTabs'
  73.         ];
  74.     }
  75.     /**
  76.      * @param DocumentTabsEvent $event
  77.      * @return void
  78.      */
  79.     public function invoiceTabs(DocumentTabsEvent $event)
  80.     {
  81.         if (!$this->security->isGranted('ROLE__GOCARDLESS__READ_ONLY')) {
  82.             return;
  83.         }
  84.         $invoiceId $this->requestStack->getMainRequest()->attributes->get('id');
  85.         $invoice $this->invoiceRepo->find($invoiceId);
  86.         $customerMapping $this->customerMappingRepo->findOneBy([
  87.             'customer' => $invoice->getCustomer()
  88.         ]);
  89.         if (!$customerMapping) {
  90.             return;
  91.         }
  92.         $directDebits $this->directDebitRepo->count([
  93.             'invoice' => $invoice
  94.         ]);
  95.         $badge '';
  96.         if ($directDebits) {
  97.             $badge '<span class="badge badge-pill badge-info ml-1">' $directDebits '</span>';
  98.         }
  99.         $event->addTab([
  100.             'key' => 'gocardless_bundle__direct_debits',
  101.             'name' => $this->tr->trans('Direct debits', [], 'GocardlessBundle') . $badge,
  102.             'icon' => 'far fa-credit-card',
  103.             'idRequired' => true,
  104.             'path' => 'gocardless_bundle__invoice_direct_debits',
  105.             'layout' => 'view'
  106.         ]);
  107.     }
  108. }