Skip to content

Commit d7ef93a

Browse files
committed
Extract FileManager for better credit memo file management
1 parent a1fe44a commit d7ef93a

File tree

11 files changed

+134
-28
lines changed

11 files changed

+134
-28
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: php
22

33
dist: trusty
44

5-
sudo: false
5+
sudo: required
66

77
php:
88
- 7.2
@@ -70,6 +70,8 @@ script:
7070
- composer validate --strict
7171
- bin/phpstan.phar analyse -c phpstan.neon -l max src/
7272

73+
- bin/phpspec run
74+
- bin/phpunit
7375
- bin/behat -vvv --no-interaction || bin/behat -vvv --no-interaction --rerun
7476

7577
after_failure:

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
colors="true"
77
bootstrap="vendor/autoload.php">
88
<testsuites>
9-
<testsuite name="AcmeSyliusExamplePlugin Test Suite">
9+
<testsuite name="SyliusRefundPlugin Test Suite">
1010
<directory>tests</directory>
1111
</testsuite>
1212
</testsuites>

spec/Sender/CreditMemoEmailSenderSpec.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
use PhpSpec\ObjectBehavior;
88
use Sylius\Component\Mailer\Sender\SenderInterface;
99
use Sylius\RefundPlugin\Entity\CreditMemoInterface;
10+
use Sylius\RefundPlugin\File\FileManagerInterface;
1011
use Sylius\RefundPlugin\Generator\CreditMemoPdfFileGeneratorInterface;
1112
use Sylius\RefundPlugin\Model\CreditMemoPdf;
1213
use Sylius\RefundPlugin\Sender\CreditMemoEmailSenderInterface;
13-
use Symfony\Component\Filesystem\Filesystem;
1414

1515
final class CreditMemoEmailSenderSpec extends ObjectBehavior
1616
{
1717
function let(
1818
CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
19-
Filesystem $filesystem,
20-
SenderInterface $sender
19+
SenderInterface $sender,
20+
FileManagerInterface $fileManager
2121
): void {
22-
$this->beConstructedWith($creditMemoPdfFileGenerator, $filesystem, $sender, '/web/media/temp/credit-memos/');
22+
$this->beConstructedWith($creditMemoPdfFileGenerator, $sender, $fileManager);
2323
}
2424

2525
function it_implements_credit_memo_email_sender_interface()
@@ -29,26 +29,25 @@ function it_implements_credit_memo_email_sender_interface()
2929

3030
function it_sends_email_with_credit_memo_to_customer(
3131
CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
32-
Filesystem $filesystem,
3332
SenderInterface $sender,
33+
FileManagerInterface $fileManager,
3434
CreditMemoInterface $creditMemo
3535
): void {
3636
$creditMemo->getId()->willReturn(1);
3737

3838
$creditMemoPdf = new CreditMemoPdf('credit-memo.pdf', 'I am credit memo number #2018/10/000444');
3939
$creditMemoPdfFileGenerator->generate(1)->willReturn($creditMemoPdf);
4040

41-
$filesystem
42-
->dumpFile('/web/media/temp/credit-memos/credit-memo.pdf', 'I am credit memo number #2018/10/000444')
43-
->shouldBeCalled()
44-
;
41+
$fileManager->createWithContent('credit-memo.pdf', 'I am credit memo number #2018/10/000444')->shouldBeCalled();
42+
43+
$fileManager->getBaseDirectory()->willReturn('/base/directory/');
4544

4645
$sender
47-
->send('units_refunded', ['[email protected]'], ['creditMemo' => $creditMemo], ['/web/media/temp/credit-memos/credit-memo.pdf'])
46+
->send('units_refunded', ['[email protected]'], ['creditMemo' => $creditMemo], ['/base/directory/credit-memo.pdf'])
4847
->shouldBeCalled()
4948
;
5049

51-
$filesystem->remove('/web/media/temp/credit-memos/credit-memo.pdf')->shouldBeCalled();
50+
$fileManager->remove('credit-memo.pdf')->shouldBeCalled();
5251

5352
$this->send($creditMemo, '[email protected]');
5453
}

src/File/FileManager.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\RefundPlugin\File;
6+
7+
final class FileManager implements FileManagerInterface
8+
{
9+
/** @var string */
10+
private $baseDirectory;
11+
12+
public function __construct(string $baseDirectory = '')
13+
{
14+
$this->baseDirectory = $baseDirectory;
15+
}
16+
17+
public function createWithContent(string $fileName, string $content): void
18+
{
19+
file_put_contents($this->baseDirectory . $fileName, $content);
20+
}
21+
22+
public function remove(string $fileName): void
23+
{
24+
unlink($this->baseDirectory . $fileName);
25+
}
26+
27+
public function getBaseDirectory(): string
28+
{
29+
return $this->baseDirectory;
30+
}
31+
}

src/File/FileManagerInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\RefundPlugin\File;
6+
7+
interface FileManagerInterface
8+
{
9+
public function createWithContent(string $fileName, string $content): void;
10+
11+
public function remove(string $fileName): void;
12+
13+
public function getBaseDirectory(): string;
14+
}

src/Listener/CreditMemoGeneratedEventListener.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function __invoke(CreditMemoGenerated $event): void
4848
throw OrderNotFound::withOrderNumber($event->orderNumber());
4949
}
5050

51+
assert($order->getCustomer() !== null);
52+
5153
$this->creditMemoEmailSender->send($creditMemo, $order->getCustomer()->getEmail());
5254
}
5355
}

src/Resources/config/services.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@
7474

7575
<service id="Sylius\RefundPlugin\Sender\CreditMemoEmailSender">
7676
<argument type="service" id="Sylius\RefundPlugin\Generator\CreditMemoPdfFileGenerator" />
77-
<argument type="service" id="filesystem" />
7877
<argument type="service" id="sylius.email_sender" />
78+
<argument type="service" id="Sylius\RefundPlugin\File\FileManager" />
79+
</service>
80+
81+
<service id="Sylius\RefundPlugin\File\FileManager">
7982
<argument type="string">%kernel.project_dir%/web/media/credit-memos/</argument>
8083
</service>
8184
</services>

src/Sender/CreditMemoEmailSender.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,46 @@
66

77
use Sylius\Component\Mailer\Sender\SenderInterface;
88
use Sylius\RefundPlugin\Entity\CreditMemoInterface;
9+
use Sylius\RefundPlugin\File\FileManagerInterface;
910
use Sylius\RefundPlugin\Generator\CreditMemoPdfFileGeneratorInterface;
10-
use Symfony\Component\Filesystem\Filesystem;
1111

1212
final class CreditMemoEmailSender implements CreditMemoEmailSenderInterface
1313
{
14+
private const UNITS_REFUNDED = 'units_refunded';
15+
1416
/** @var CreditMemoPdfFileGeneratorInterface */
1517
private $creditMemoPdfFileGenerator;
1618

17-
/** @var Filesystem */
18-
private $filesystem;
19-
2019
/** @var SenderInterface */
2120
private $sender;
2221

23-
/** @var string */
24-
private $temporaryCreditMemoPath;
22+
/** @var FileManagerInterface */
23+
private $fileManager;
2524

2625
public function __construct(
2726
CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
28-
Filesystem $filesystem,
2927
SenderInterface $sender,
30-
string $temporaryCreditMemoPath
28+
FileManagerInterface $fileManager
3129
) {
3230
$this->creditMemoPdfFileGenerator = $creditMemoPdfFileGenerator;
33-
$this->filesystem = $filesystem;
3431
$this->sender = $sender;
35-
$this->temporaryCreditMemoPath = $temporaryCreditMemoPath;
32+
$this->fileManager = $fileManager;
3633
}
3734

3835
public function send(CreditMemoInterface $creditMemo, string $recipient): void
3936
{
4037
$creditMemoPdfFile = $this->creditMemoPdfFileGenerator->generate($creditMemo->getId());
4138

42-
$filePath = $this->temporaryCreditMemoPath.$creditMemoPdfFile->filename();
43-
$this->filesystem->dumpFile($filePath, $creditMemoPdfFile->content() );
39+
$filePath = $creditMemoPdfFile->filename();
40+
$this->fileManager->createWithContent($filePath, $creditMemoPdfFile->content());
4441

45-
$this->sender->send('units_refunded', [$recipient], ['creditMemo' => $creditMemo], [$filePath]);
42+
$this->sender->send(
43+
self::UNITS_REFUNDED,
44+
[$recipient],
45+
['creditMemo' => $creditMemo],
46+
[$this->fileManager->getBaseDirectory() . $filePath]
47+
);
4648

47-
$this->filesystem->remove($filePath);
49+
$this->fileManager->remove($filePath);
4850
}
4951
}

tests/Application/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
!/web/app.php
88
!/web/app_dev.php
99
!/web/app_test.php
10+
!/web/media/credit-memos/

tests/Application/app/config/config_test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ imports:
44
doctrine:
55
dbal:
66
path: "%kernel.project_dir%/var/db_test.sql"
7+
8+
knp_snappy:
9+
pdf:
10+
binary: "%kernel.project_dir%/app/etc/wkhtmltopdf"

0 commit comments

Comments
 (0)