Skip to content

Commit

Permalink
[EmailSender][Admin] Send the Credit Memo email without attachment if…
Browse files Browse the repository at this point in the history
… pdf generator is turned off
  • Loading branch information
Rafikooo committed Apr 21, 2022
1 parent 51d5967 commit 761bea0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
35 changes: 29 additions & 6 deletions spec/Sender/CreditMemoEmailSenderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,24 @@

final class CreditMemoEmailSenderSpec extends ObjectBehavior
{
function let(
function it_implements_credit_memo_email_sender_interface(
CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
SenderInterface $sender,
FileManagerInterface $fileManager
): void {
$this->beConstructedWith($creditMemoPdfFileGenerator, $sender, $fileManager);
}
$this->beConstructedWith($creditMemoPdfFileGenerator, $sender, $fileManager, true);

function it_implements_credit_memo_email_sender_interface()
{
$this->shouldImplement(CreditMemoEmailSenderInterface::class);
}

function it_sends_email_with_credit_memo_to_customer(
function it_sends_an_email_with_credit_memo_and_pdf_file_attachment_to_customer(
CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
SenderInterface $sender,
FileManagerInterface $fileManager,
CreditMemoInterface $creditMemo
): void {
$this->beConstructedWith($creditMemoPdfFileGenerator, $sender, $fileManager, true);

$creditMemo->getId()->willReturn('7903c83a-4c5e-4bcf-81d8-9dc304c6a353');

$creditMemoPdf = new CreditMemoPdf('credit-memo.pdf', 'I am credit memo number #2018/10/000444');
Expand All @@ -63,4 +62,28 @@ function it_sends_email_with_credit_memo_to_customer(

$this->send($creditMemo, 'john@example.com');
}

function it_sends_an_email_with_credit_memo_to_customer_without_pdf_file_attachment_if_pdf_file_generator_is_disabled(
CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
SenderInterface $sender,
FileManagerInterface $fileManager,
CreditMemoInterface $creditMemo
): void {
$this->beConstructedWith($creditMemoPdfFileGenerator, $sender, $fileManager, false);

$creditMemo->getId()->shouldNotBeCalled();
$creditMemoPdfFileGenerator->generate('creditMemoId')->shouldNotBeCalled();

$fileManager->createWithContent('credit-memo.pdf', 'content')->shouldNotBeCalled();
$fileManager->realPath('credit-memo.pdf')->shouldNotBeCalled();

$sender
->send('units_refunded', ['john@example.com'], ['creditMemo' => $creditMemo], [])
->shouldBeCalled()
;

$fileManager->remove('credit-memo.pdf')->shouldNotBeCalled();

$this->send($creditMemo, 'john@example.com');
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<argument type="service" id="Sylius\RefundPlugin\Generator\CreditMemoPdfFileGeneratorInterface" />
<argument type="service" id="sylius.email_sender" />
<argument type="service" id="Sylius\RefundPlugin\File\TemporaryFileManager" />
<argument type="string">%sylius_refund.pdf_generator.enabled%</argument>
</service>
<service id="Sylius\RefundPlugin\Sender\CreditMemoEmailSender" alias="Sylius\RefundPlugin\Sender\CreditMemoEmailSenderInterface">
<deprecated>The "%alias_id%" service alias is deprecated and will be removed in RefundPlugin 1.0, use Sylius\RefundPlugin\Sender\CreditMemoEmailSenderInterface instead.</deprecated>
Expand Down
32 changes: 15 additions & 17 deletions src/Sender/CreditMemoEmailSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,34 @@ final class CreditMemoEmailSender implements CreditMemoEmailSenderInterface
{
private const UNITS_REFUNDED = 'units_refunded';

private CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator;

private SenderInterface $sender;

private FileManagerInterface $fileManager;

public function __construct(
CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
SenderInterface $sender,
FileManagerInterface $fileManager
private CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
private SenderInterface $sender,
private FileManagerInterface $fileManager,
private bool $hasEnabledPdfFileGenerator = true
) {
$this->creditMemoPdfFileGenerator = $creditMemoPdfFileGenerator;
$this->sender = $sender;
$this->fileManager = $fileManager;
}

public function send(CreditMemoInterface $creditMemo, string $recipient): void
{
$creditMemoPdfFile = $this->creditMemoPdfFileGenerator->generate($creditMemo->getId());
if ($this->hasEnabledPdfFileGenerator) {
$creditMemoPdfFile = $this->creditMemoPdfFileGenerator->generate($creditMemo->getId());

$filePath = $creditMemoPdfFile->filename();
$this->fileManager->createWithContent($filePath, $creditMemoPdfFile->content());

$filePath = $creditMemoPdfFile->filename();
$this->fileManager->createWithContent($filePath, $creditMemoPdfFile->content());
$attachments = [$this->fileManager->realPath($filePath)];
}

$this->sender->send(
self::UNITS_REFUNDED,
[$recipient],
['creditMemo' => $creditMemo],
[$this->fileManager->realPath($filePath)]
$attachments ?? []
);

$this->fileManager->remove($filePath);
if ($this->hasEnabledPdfFileGenerator) {
$this->fileManager->remove($filePath ?? '');
}
}
}

0 comments on commit 761bea0

Please sign in to comment.