Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(invoicing): send email w/ attachment included #40

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 0 additions & 4 deletions .github/workflows/sylius.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ jobs:
- 8.0
- 8.2
sylius:
- 1.10.0
- 1.12.0
symfony:
- 5.4
- 6.2
node:
- 14.x
exclude:
-
sylius: 1.10.0
symfony: 6.2
-
php: '8.0'
symfony: 6.2
Expand Down
1 change: 1 addition & 0 deletions 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 Down
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'));
}
}
6 changes: 6 additions & 0 deletions src/DependencyInjection/SynoliaSyliusMailTesterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Synolia\SyliusMailTesterPlugin\DependencyInjection;

use Sylius\InvoicingPlugin\SyliusInvoicingPlugin;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
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 +28,9 @@ public function load(array $config, ContainerBuilder $container): void
->registerForAutoconfiguration(ResolvableFormTypeInterface::class)
->addTag(ResolvableFormTypeResolverCompilerPass::TAG_ID)
;

if (!class_exists(SyliusInvoicingPlugin::class)) {
$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()];
}
}
Loading