src/WellCommerce/Bundle/OrderBundle/EventListener/InvoiceSubscriber.php line 59

Open in your IDE?
  1. <?php
  2. declare(strict_types=0);
  3. /*
  4.  * WellCommerce Foundation
  5.  *
  6.  * This file is part of the WellCommerce package.
  7.  *
  8.  * (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
  9.  *
  10.  * For the full copyright and license information,
  11.  * please view the LICENSE file that was distributed with this source code.
  12.  */
  13. namespace WellCommerce\Bundle\OrderBundle\EventListener;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Messenger\Stamp\DelayStamp;
  16. use WellCommerce\Bundle\AppBundle\Entity\Shop;
  17. use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
  18. use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
  19. use WellCommerce\Bundle\OrderBundle\Entity\Invoice;
  20. use WellCommerce\Bundle\OrderBundle\Service\Invoice\Processor\InvoiceProcessorCollection;
  21. use WellCommerce\Bundle\OrderBundle\Service\Invoice\Processor\InvoiceProcessorInterface;
  22. use WellCommerce\Bundle\OrderBundle\Service\Messenger\InvoiceMessage;
  23. use WellCommerce\Component\Form\Event\FormEvent;
  24. /**
  25.  * Class InvoiceSubscriber
  26.  *
  27.  * @author  Adam Piotrowski <adam@wellcommerce.org>
  28.  */
  29. class InvoiceSubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
  30. {
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             'admin.shop.pre_form_init' => ['onShopFormAdminInit'],
  35.             'invoice.post_create'      => ['onInvoicePostCreate'],
  36.         ];
  37.     }
  38.     public function onInvoicePostCreate(EntityEvent $entityEvent)
  39.     {
  40.         $invoice $entityEvent->getEntity();
  41.         if ($invoice instanceof Invoice) {
  42.             $enabled = (bool) $this->getKernel()->getContainer()->getParameter('messenger_invoice_message_enabled');
  43.             $delay   $this->getKernel()->getContainer()->getParameter('messenger_invoice_message_delay');
  44.             if (true === $enabled) {
  45.                 $this->getMessageBus()->dispatch(new InvoiceMessage($invoice->getId()), [
  46.                     new DelayStamp($delay),
  47.                 ]);
  48.             }
  49.         }
  50.     }
  51.     public function onShopFormAdminInit(FormEvent $event)
  52.     {
  53.         $resource $event->getResource();
  54.         if ($resource instanceof Shop) {
  55.             $form    $event->getForm();
  56.             $builder $event->getFormBuilder();
  57.             $invoiceData $form->addChild($builder->getElement('nested_fieldset', [
  58.                 'name'  => 'invoice_data',
  59.                 'label' => 'invoice.fieldset.settings',
  60.             ]));
  61.             $invoiceData->addChild($builder->getElement('text_field', [
  62.                 'name'    => 'invoiceMaturity',
  63.                 'label'   => 'invoice.label.maturity',
  64.                 'default' => 7,
  65.             ]));
  66.             $options       = [];
  67.             $processors    $this->locator->get('invoice_processor.collection');
  68.             $processorKeys $processors->getKeys();
  69.             /** @var InvoiceProcessorInterface $processor */
  70.             foreach ($processors as $processor) {
  71.                 $processorName           $processor->getAlias();
  72.                 $options[$processorName] = $processorName;
  73.             }
  74.             $defaultProcessor reset($processorKeys);
  75.             $invoiceData->addChild($builder->getElement('select', [
  76.                 'name'    => 'invoiceProcessor',
  77.                 'label'   => 'invoice.label.processor',
  78.                 'default' => $defaultProcessor,
  79.                 'options' => $options,
  80.             ]));
  81.         }
  82.     }
  83.     public static function getSubscribedServices()
  84.     {
  85.         return array_merge(parent::getSubscribedServices(), [
  86.             'invoice_processor.collection' => InvoiceProcessorCollection::class,
  87.         ]);
  88.     }
  89. }