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 db145f1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ jobs:
-
uses: symfonycorp/security-checker-action@v5
if: 'always() && steps.end-of-setup.outcome == ''success'''
continue-on-error: true
3 changes: 2 additions & 1 deletion ruleset/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ parameters:
# Makes PHPStan crash
- ../src/DependencyInjection/Configuration.php
- ../src/Migrations/
- ../src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php

# Test dependencies
- ../tests/Application
Expand All @@ -21,4 +22,4 @@ parameters:
- '#Call to method [a-zA-Z0-9]+\(\) on an unknown class Sylius\\Plus#'
- '#Parameter \$[a-zA-Z0-9]+ of anonymous function has invalid typehint type Sylius\\Plus#'
- "#^Parameter \\$returnRequestInterface of anonymous function has invalid type Sylius\\\\Plus\\\\Returns\\\\Domain\\\\Model\\\\ReturnRequestInterface\\.$#"
- "#^Call to an undefined method Symfony\\\\Component\\\\HttpFoundation\\\\Session\\\\SessionInterface\\:\\:getFlashBag\\(\\)\\.$#"
- "#^Call to an undefined method Symfony\\\\Component\\\\HttpFoundation\\\\Session\\\\SessionInterface\\:\\:getFlashBag\\(\\)\\.$#"
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->getAttachments($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);
}
}
}
9 changes: 9 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,12 @@ public function getFormType(string $emailKey): ResolvableFormTypeInterface
{
return $this;
}

/**
* @inheritdoc
*/
public function getAttachments(FormInterface $form): array
{
return [];
}
}
55 changes: 55 additions & 0 deletions src/Form/Type/Plugin/InvoicingPlugin/InvoiceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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\DependencyInjection\Attribute\Autowire;
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,
#[Autowire('%sylius_invoicing.pdf_generator.enabled%')]
private $hasEnabledPdfFileGenerator,
) {
}

/**
* @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 getAttachments(FormInterface $form): array
{
$invoice = $form->get('invoice')->getData();
if (!$invoice instanceof Invoice) {
return [];
}

if (!$this->hasEnabledPdfFileGenerator) {
return [];
}

return [$this->invoiceFileProvider->provide($invoice)->fullPath()];
}
}
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 db145f1

Please sign in to comment.