Skip to content

Commit

Permalink
Merge pull request #340 from GSadee/improvements
Browse files Browse the repository at this point in the history
Minor improvements and clean up
  • Loading branch information
mpysiak authored Nov 20, 2024
2 parents 30315fb + 525fb8e commit e643edc
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 42 deletions.
45 changes: 38 additions & 7 deletions spec/CommandHandler/SendInvoiceEmailHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace spec\Sylius\InvoicingPlugin\CommandHandler;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
Expand Down Expand Up @@ -41,16 +42,45 @@ function it_requests_an_email_with_an_invoice_to_be_sent(
CustomerInterface $customer,
): void {
$orderRepository->findOneByNumber('0000001')->willReturn($order);

$invoiceRepository->findOneByOrder($order)->willReturn($invoice);

$order->getCustomer()->willReturn($customer);

$customer->getEmail()->willReturn('[email protected]');
$invoiceRepository->findOneByOrder($order)->willReturn($invoice);

$emailSender->sendInvoiceEmail($invoice, '[email protected]')->shouldBeCalled();

$this->__invoke(new SendInvoiceEmail('0000001', new \DateTime('now')));
$this(new SendInvoiceEmail('0000001'));
}

function it_does_not_request_an_email_to_be_sent_if_order_was_not_found(
InvoiceRepositoryInterface $invoiceRepository,
OrderRepositoryInterface $orderRepository,
InvoiceEmailSenderInterface $emailSender,
CustomerInterface $customer,
): void {
$orderRepository->findOneByNumber('0000001')->willReturn(null);

$invoiceRepository->findOneByOrder(Argument::any())->shouldNotBeCalled();
$customer->getEmail()->shouldNotBeCalled();
$emailSender->sendInvoiceEmail(Argument::any(), Argument::any())->shouldNotBeCalled();

$this(new SendInvoiceEmail('0000001'));
}

function it_does_not_request_an_email_to_be_sent_if_customer_was_not_found(
InvoiceRepositoryInterface $invoiceRepository,
OrderRepositoryInterface $orderRepository,
InvoiceEmailSenderInterface $emailSender,
OrderInterface $order,
CustomerInterface $customer,
): void {
$orderRepository->findOneByNumber('0000001')->willReturn($order);
$order->getCustomer()->willReturn(null);

$invoiceRepository->findOneByOrder($order)->shouldNotBeCalled();
$customer->getEmail()->shouldNotBeCalled();
$emailSender->sendInvoiceEmail(Argument::any(), Argument::any())->shouldNotBeCalled();

$this(new SendInvoiceEmail('0000001'));
}

function it_does_not_request_an_email_to_be_sent_if_invoice_was_not_found(
Expand All @@ -61,11 +91,12 @@ function it_does_not_request_an_email_to_be_sent_if_invoice_was_not_found(
CustomerInterface $customer,
): void {
$orderRepository->findOneByNumber('0000001')->willReturn($order);
$order->getCustomer()->willReturn($customer);
$invoiceRepository->findOneByOrder($order)->willReturn(null);

$order->getCustomer()->shouldNotBeCalled();
$customer->getEmail()->shouldNotBeCalled();
$emailSender->sendInvoiceEmail(Argument::any(), Argument::any())->shouldNotBeCalled();

$this->__invoke(new SendInvoiceEmail('0000001', new \DateTime('now')));
$this(new SendInvoiceEmail('0000001'));
}
}
2 changes: 1 addition & 1 deletion spec/EventProducer/OrderPlacedProducerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace spec\Sylius\InvoicingPlugin\EventProducer;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Mockery;
use Mockery\MockInterface;
use PhpSpec\ObjectBehavior;
Expand Down
17 changes: 8 additions & 9 deletions src/Cli/GenerateInvoicesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@

use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\InvoicingPlugin\Creator\MassInvoicesCreatorInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'sylius-invoicing:generate-invoices',
description: 'Generates invoices for orders placed before InvoicingPlugin installation',
)]
final class GenerateInvoicesCommand extends Command
{
protected static $defaultName = 'sylius-invoicing:generate-invoices';

public function __construct(
private readonly MassInvoicesCreatorInterface $massInvoicesCreator,
private readonly OrderRepositoryInterface $orderRepository,
Expand All @@ -37,17 +40,13 @@ public function execute(InputInterface $input, OutputInterface $output): int
->createListQueryBuilder()
->andWhere('o.number IS NOT NULL')
->getQuery()
->getResult();
->getResult()
;

$this->massInvoicesCreator->__invoke($orders);

$output->writeln('Invoices generated successfully');

return 0;
}

protected function configure(): void
{
$this->setDescription('Generates invoices for orders placed before InvoicingPlugin installation');
return Command::SUCCESS;
}
}
17 changes: 10 additions & 7 deletions src/CommandHandler/SendInvoiceEmailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ public function __construct(

public function __invoke(SendInvoiceEmail $command): void
{
/** @var OrderInterface $order */
/** @var OrderInterface|null $order */
$order = $this->orderRepository->findOneByNumber($command->orderNumber());
if (null === $order) {
return;
}

/** @var InvoiceInterface|null $invoice */
$invoice = $this->invoiceRepository->findOneByOrder($order);

if (null === $invoice) {
$customer = $order->getCustomer();
if (null === $customer) {
return;
}

if (null === $order->getCustomer()) {
/** @var InvoiceInterface|null $invoice */
$invoice = $this->invoiceRepository->findOneByOrder($order);
if (null === $invoice) {
return;
}

$this->emailSender->sendInvoiceEmail($invoice, $order->getCustomer()->getEmail());
$this->emailSender->sendInvoiceEmail($invoice, $customer->getEmail());
}
}
6 changes: 2 additions & 4 deletions src/Email/InvoiceEmailSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ public function __construct(
) {
}

public function sendInvoiceEmail(
InvoiceInterface $invoice,
string $customerEmail,
): void {
public function sendInvoiceEmail(InvoiceInterface $invoice, string $customerEmail): void
{
if (!$this->hasEnabledPdfFileGenerator) {
$this->emailSender->send(Emails::INVOICE_GENERATED, [$customerEmail], ['invoice' => $invoice]);

Expand Down
6 changes: 4 additions & 2 deletions src/EventProducer/OrderPlacedProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@

namespace Sylius\InvoicingPlugin\EventProducer;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\InvoicingPlugin\Event\OrderPlaced;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Webmozart\Assert\Assert;

final class OrderPlacedProducer
{
Expand All @@ -45,12 +47,12 @@ public function postPersist(LifecycleEventArgs $event): void
public function postUpdate(LifecycleEventArgs $event): void
{
$order = $event->getObject();

if (!$order instanceof OrderInterface) {
return;
}

$entityManager = $event->getObjectManager();
Assert::isInstanceOf($entityManager, EntityManagerInterface::class);

$unitOfWork = $entityManager->getUnitOfWork();
$changeSet = $unitOfWork->getEntityChangeSet($order);
Expand Down
6 changes: 0 additions & 6 deletions src/Fixture/Factory/ShopBillingDataExampleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public function __construct(
$this->configureOptions($this->optionsResolver);
}

/**
* @inheritdoc
*/
public function create(array $options = []): ChannelInterface
{
$options = $this->optionsResolver->resolve($options);
Expand All @@ -62,9 +59,6 @@ public function create(array $options = []): ChannelInterface
return $channel;
}

/**
* @inheritdoc
*/
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver
Expand Down
6 changes: 0 additions & 6 deletions src/Fixture/ShopBillingDataFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@

final class ShopBillingDataFixture extends AbstractResourceFixture
{
/**
* @inheritdoc
*/
public function getName(): string
{
return 'shop_billing_data';
}

/**
* @inheritdoc
*/
protected function configureResourceNode(ArrayNodeDefinition $resourceNode): void
{
$resourceNode
Expand Down

0 comments on commit e643edc

Please sign in to comment.