<?php
declare(strict_types=0);
/*
* WellCommerce Foundation
*
* This file is part of the WellCommerce package.
*
* (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace WellCommerce\Bundle\OrderBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use WellCommerce\Bundle\AppBundle\Entity\Shop;
use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
use WellCommerce\Bundle\OrderBundle\Entity\Invoice;
use WellCommerce\Bundle\OrderBundle\Service\Invoice\Processor\InvoiceProcessorCollection;
use WellCommerce\Bundle\OrderBundle\Service\Invoice\Processor\InvoiceProcessorInterface;
use WellCommerce\Bundle\OrderBundle\Service\Messenger\InvoiceMessage;
use WellCommerce\Component\Form\Event\FormEvent;
/**
* Class InvoiceSubscriber
*
* @author Adam Piotrowski <adam@wellcommerce.org>
*/
class InvoiceSubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'admin.shop.pre_form_init' => ['onShopFormAdminInit'],
'invoice.post_create' => ['onInvoicePostCreate'],
];
}
public function onInvoicePostCreate(EntityEvent $entityEvent)
{
$invoice = $entityEvent->getEntity();
if ($invoice instanceof Invoice) {
$enabled = (bool) $this->getKernel()->getContainer()->getParameter('messenger_invoice_message_enabled');
$delay = $this->getKernel()->getContainer()->getParameter('messenger_invoice_message_delay');
if (true === $enabled) {
$this->getMessageBus()->dispatch(new InvoiceMessage($invoice->getId()), [
new DelayStamp($delay),
]);
}
}
}
public function onShopFormAdminInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof Shop) {
$form = $event->getForm();
$builder = $event->getFormBuilder();
$invoiceData = $form->addChild($builder->getElement('nested_fieldset', [
'name' => 'invoice_data',
'label' => 'invoice.fieldset.settings',
]));
$invoiceData->addChild($builder->getElement('text_field', [
'name' => 'invoiceMaturity',
'label' => 'invoice.label.maturity',
'default' => 7,
]));
$options = [];
$processors = $this->locator->get('invoice_processor.collection');
$processorKeys = $processors->getKeys();
/** @var InvoiceProcessorInterface $processor */
foreach ($processors as $processor) {
$processorName = $processor->getAlias();
$options[$processorName] = $processorName;
}
$defaultProcessor = reset($processorKeys);
$invoiceData->addChild($builder->getElement('select', [
'name' => 'invoiceProcessor',
'label' => 'invoice.label.processor',
'default' => $defaultProcessor,
'options' => $options,
]));
}
}
public static function getSubscribedServices()
{
return array_merge(parent::getSubscribedServices(), [
'invoice_processor.collection' => InvoiceProcessorCollection::class,
]);
}
}