Skip to content

Commit

Permalink
feat(invoicing): send email w/ attachment included; closes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
maxperei committed Mar 6, 2024
1 parent 6d8ae30 commit 0b598a4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Controller/MailTesterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function sendMail(Request $request, array $mailTester, SenderInterface $
}
}
if ($mailTester['subjects'] !== ChoiceSubjectsType::EVERY_SUBJECTS) {
$sender->send($formData['subjects'], [$formData['recipient']], $this->getMailData($form, 'form_subject_chosen'));
$sender->send($formData['subjects'], [$formData['recipient']], $this->getMailData($form, 'form_subject_chosen'), $this->getAttachments($form));
}

$request->getSession()->getFlashBag()->add('success', $this->translator->trans('sylius.ui.admin.mail_tester.success'));
Expand Down Expand Up @@ -134,4 +134,14 @@ private function getMailData(FormInterface $form, string $type): array

return $emailData;
}

private function getAttachments(FormInterface $form): array
{
$formType = $form->getData()['form_subject'];
if (!$formType instanceof AbstractType) {
return [];
}

return $formType->attachments($form->get('form_subject_chosen'));
}
}
5 changes: 5 additions & 0 deletions src/DependencyInjection/SynoliaSyliusMailTesterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Synolia\SyliusMailTesterPlugin\DependencyInjection\Pass\ResolvableFormTypeResolverCompilerPass;
use Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType;
use Synolia\SyliusMailTesterPlugin\Resolver\ResolvableFormTypeInterface;

final class SynoliaSyliusMailTesterExtension extends Extension
Expand All @@ -26,5 +27,9 @@ public function load(array $config, ContainerBuilder $container): void
->registerForAutoconfiguration(ResolvableFormTypeInterface::class)
->addTag(ResolvableFormTypeResolverCompilerPass::TAG_ID)
;

if (!class_exists('Sylius\InvoicingPlugin\SyliusInvoicingPlugin')) {
$container->removeDefinition(InvoiceType::class);
}
}
}
6 changes: 6 additions & 0 deletions src/Form/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Synolia\SyliusMailTesterPlugin\Form\Type;

use Symfony\Component\Form\FormInterface;
use Synolia\SyliusMailTesterPlugin\Resolver\ResolvableFormTypeInterface;

abstract class AbstractType extends \Symfony\Component\Form\AbstractType implements ResolvableFormTypeInterface
Expand All @@ -25,4 +26,9 @@ public function getFormType(string $emailKey): ResolvableFormTypeInterface
{
return $this;
}

public function attachments(FormInterface $form): array
{
return [];
}
}
48 changes: 48 additions & 0 deletions src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin;

use Sylius\InvoicingPlugin\Entity\Invoice;
use Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Synolia\SyliusMailTesterPlugin\Form\Type\AbstractType;

final class InvoiceType extends AbstractType
{
/** @var string */
protected static $syliusEmailKey = 'invoice_generated';

public function __construct(
private InvoiceFileProviderInterface $invoiceFileProvider,

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 6.2.*

Parameter $invoiceFileProvider of method Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::__construct() has invalid type Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 6.2.*

Property Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::$invoiceFileProvider has unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface as its type.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 6.2.*

Parameter $invoiceFileProvider of method Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::__construct() has invalid type Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 6.2.*

Property Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::$invoiceFileProvider has unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface as its type.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Parameter $invoiceFileProvider of method Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::__construct() has invalid type Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Property Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::$invoiceFileProvider has unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface as its type.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Parameter $invoiceFileProvider of method Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::__construct() has invalid type Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Property Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::$invoiceFileProvider has unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface as its type.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Parameter $invoiceFileProvider of method Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::__construct() has invalid type Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Property Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::$invoiceFileProvider has unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface as its type.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Parameter $invoiceFileProvider of method Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::__construct() has invalid type Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Property Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::$invoiceFileProvider has unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface as its type.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 5.4.*

Parameter $invoiceFileProvider of method Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::__construct() has invalid type Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 5.4.*

Property Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::$invoiceFileProvider has unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface as its type.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 5.4.*

Parameter $invoiceFileProvider of method Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::__construct() has invalid type Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 20 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 5.4.*

Property Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::$invoiceFileProvider has unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface as its type.
) {
}

/**
* @inheritdoc
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
if (!class_exists(Invoice::class)) {
return;
}

$builder->add('invoice', EntityType::class, [
'class' => Invoice::class,
'choice_label' => 'number',
]);
}

public function attachments(FormInterface $form): array
{
$invoice = $form->get('invoice')->getData();
if (!$invoice instanceof Invoice) {

Check failure on line 42 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 6.2.*

Class Sylius\InvoicingPlugin\Entity\Invoice not found.

Check failure on line 42 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 6.2.*

Class Sylius\InvoicingPlugin\Entity\Invoice not found.

Check failure on line 42 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Class Sylius\InvoicingPlugin\Entity\Invoice not found.

Check failure on line 42 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Class Sylius\InvoicingPlugin\Entity\Invoice not found.

Check failure on line 42 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Class Sylius\InvoicingPlugin\Entity\Invoice not found.

Check failure on line 42 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Class Sylius\InvoicingPlugin\Entity\Invoice not found.

Check failure on line 42 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 5.4.*

Class Sylius\InvoicingPlugin\Entity\Invoice not found.

Check failure on line 42 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 5.4.*

Class Sylius\InvoicingPlugin\Entity\Invoice not found.
return [];
}

return [$this->invoiceFileProvider->provide($invoice)->fullPath()];

Check failure on line 46 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 6.2.*

Call to method provide() on an unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 46 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 6.2.*

Call to method provide() on an unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 46 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Call to method provide() on an unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 46 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Call to method provide() on an unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 46 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Call to method provide() on an unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 46 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 5.4.*

Call to method provide() on an unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 46 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 5.4.*

Call to method provide() on an unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.

Check failure on line 46 in src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

View workflow job for this annotation

GitHub Actions / PHP 8 Symfony 5.4.*

Call to method provide() on an unknown class Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface.
}
}
1 change: 1 addition & 0 deletions tests/PHPUnit/Resolver/FormTypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ public function provideEmailKeyAndExpectedForm(): \Generator
yield '[Plus] Return request resolution changed' => ['sylius_plus_return_request_resolution_changed', \Synolia\SyliusMailTesterPlugin\Form\Type\SyliusPlusReturnRequestType::class];
yield '[Plus] Return request repaired items sent' => ['sylius_plus_return_request_repaired_items_sent', \Synolia\SyliusMailTesterPlugin\Form\Type\SyliusPlusReturnRequestType::class];
yield '[Plus] Loyalti purchase coupon' => ['sylius_plus_loyalty_purchase_coupon', \Synolia\SyliusMailTesterPlugin\Form\Type\SyliusPlusLoyaltyPurchaseCouponType::class];
yield 'Invoice generated' => ['invoice_generated', \Synolia\SyliusMailTesterPlugin\Form\Type\Plugin\InvoicingPlugin\InvoiceType::class];
}
}

0 comments on commit 0b598a4

Please sign in to comment.