From 49bb9980b89e23306636cb760597bf4f48806af7 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Thu, 20 Jan 2022 18:41:55 +0100 Subject: [PATCH] added invoice model to invoice created event (#3079) * added model to InvoiceCreatedEvent * allow to switch formatter locale --- src/Command/InvoiceCreateCommand.php | 9 ++- src/Event/InvoiceCreatedEvent.php | 13 ++- src/Invoice/DefaultInvoiceFormatter.php | 79 +++++++++---------- src/Invoice/InvoiceFormatter.php | 48 +++-------- src/Invoice/ServiceInvoice.php | 2 +- tests/Event/InvoiceCreatedEventTest.php | 6 +- .../Actions/InvoiceSubscriberTest.php | 23 ++++++ tests/Invoice/DebugFormatter.php | 52 +++++------- 8 files changed, 109 insertions(+), 123 deletions(-) create mode 100644 tests/EventSubscriber/Actions/InvoiceSubscriberTest.php diff --git a/src/Command/InvoiceCreateCommand.php b/src/Command/InvoiceCreateCommand.php index f55e8dac..2b845424 100644 --- a/src/Command/InvoiceCreateCommand.php +++ b/src/Command/InvoiceCreateCommand.php @@ -337,9 +337,10 @@ class InvoiceCreateCommand extends Command return $invoices; } - private function saveInvoicePreview(Response $response) + private function saveInvoicePreview(Response $response): string { $filename = uniqid('invoice_'); + $directory = rtrim($this->previewDirectory, '/') . '/'; if ($response->headers->has('Content-Disposition')) { $disposition = $response->headers->get('Content-Disposition'); @@ -360,12 +361,12 @@ class InvoiceCreateCommand extends Command if ($response instanceof BinaryFileResponse) { $file = $response->getFile(); - $file->move($this->previewDirectory, $filename); + $file->move($directory, $filename); } else { - (new Filesystem())->dumpFile($this->previewDirectory . $filename, $response->getContent()); + (new Filesystem())->dumpFile($directory . $filename, $response->getContent()); } - return $this->previewDirectory . $filename; + return $directory . $filename; } /** diff --git a/src/Event/InvoiceCreatedEvent.php b/src/Event/InvoiceCreatedEvent.php index eedc4404..f986c8e8 100644 --- a/src/Event/InvoiceCreatedEvent.php +++ b/src/Event/InvoiceCreatedEvent.php @@ -10,22 +10,27 @@ namespace App\Event; use App\Entity\Invoice; +use App\Invoice\InvoiceModel; use Symfony\Contracts\EventDispatcher\Event; final class InvoiceCreatedEvent extends Event { - /** - * @var Invoice - */ private $invoice; + private $model; - public function __construct(Invoice $invoice) + public function __construct(Invoice $invoice, InvoiceModel $model) { $this->invoice = $invoice; + $this->model = $model; } public function getInvoice(): Invoice { return $this->invoice; } + + public function getInvoiceModel(): InvoiceModel + { + return $this->model; + } } diff --git a/src/Invoice/DefaultInvoiceFormatter.php b/src/Invoice/DefaultInvoiceFormatter.php index 870d1bba..69c2901c 100644 --- a/src/Invoice/DefaultInvoiceFormatter.php +++ b/src/Invoice/DefaultInvoiceFormatter.php @@ -14,74 +14,71 @@ use App\Utils\LocaleFormatter; final class DefaultInvoiceFormatter implements InvoiceFormatter { + private $locale; + private $formats; /** - * @var LocaleFormatter + * @var LocaleFormatter|null */ private $formatter; public function __construct(LanguageFormattings $formats, string $locale) { - $this->formatter = new LocaleFormatter($formats, $locale); + $this->formats = $formats; + $this->locale = $locale; } - /** - * @param \DateTime $date - * @return mixed - */ - public function getFormattedDateTime(\DateTime $date) + private function getFormatter(): LocaleFormatter { - return $this->formatter->dateShort($date); + if ($this->formatter === null) { + $this->formatter = new LocaleFormatter($this->formats, $this->locale); + } + + return $this->formatter; } - /** - * @param \DateTime $date - * @return mixed - */ - public function getFormattedTime(\DateTime $date) + public function getFormattedDateTime(\DateTime $date): string { - return $this->formatter->time($date); + return $this->getFormatter()->dateShort($date); } - /** - * @param \DateTime $date - * @return mixed - */ - public function getFormattedMonthName(\DateTime $date) + public function getFormattedTime(\DateTime $date): string { - return $this->formatter->monthName($date); + return $this->getFormatter()->time($date); } - /** - * @param float|int $amount - * @param string|null $currency - * @param bool $withCurrency - * @return string - */ - public function getFormattedMoney($amount, ?string $currency, bool $withCurrency = true) + public function getFormattedMonthName(\DateTime $date): string { - return $this->formatter->money($amount, $currency, $withCurrency); + return $this->getFormatter()->monthName($date); } - /** - * @param int $seconds - * @return mixed - */ - public function getFormattedDuration($seconds) + public function getFormattedMoney(float $amount, ?string $currency, bool $withCurrency = true): string { - return $this->formatter->duration($seconds); + return $this->getFormatter()->money($amount, $currency, $withCurrency); } - /** - * @param int $seconds - * @return mixed - */ - public function getFormattedDecimalDuration($seconds) + public function getFormattedDuration(int $seconds): string { - return $this->formatter->durationDecimal($seconds); + return $this->getFormatter()->duration($seconds); + } + + public function getFormattedDecimalDuration(int $seconds): string + { + return $this->getFormatter()->durationDecimal($seconds); } public function getCurrencySymbol(string $currency): string { - return $this->formatter->currency($currency); + return $this->getFormatter()->currency($currency); + } + + public function getLocale(): string + { + return $this->locale; + } + + public function setLocale(string $locale): void + { + $this->locale = $locale; + $this->formatter = null; } } diff --git a/src/Invoice/InvoiceFormatter.php b/src/Invoice/InvoiceFormatter.php index d5279ada..73df9958 100644 --- a/src/Invoice/InvoiceFormatter.php +++ b/src/Invoice/InvoiceFormatter.php @@ -16,49 +16,21 @@ use DateTime; */ interface InvoiceFormatter { - /** - * @param DateTime $date - * @return mixed - */ - public function getFormattedDateTime(DateTime $date); + public function getLocale(): string; - /** - * @param DateTime $date - * @return mixed - */ - public function getFormattedTime(DateTime $date); + public function setLocale(string $locale): void; - /** - * @param int|float $amount - * @param string|null $currency - * @param bool $withCurrency - * @return string - */ - public function getFormattedMoney($amount, ?string $currency, bool $withCurrency = true); + public function getFormattedDateTime(DateTime $date): string; - /** - * @param DateTime $date - * @return mixed - */ - public function getFormattedMonthName(DateTime $date); + public function getFormattedTime(DateTime $date): string; - /** - * @param int $seconds - * @return mixed - */ - public function getFormattedDuration($seconds); + public function getFormattedMoney(float $amount, ?string $currency, bool $withCurrency = true): string; - /** - * @param int $seconds - * @return mixed - */ - public function getFormattedDecimalDuration($seconds); + public function getFormattedMonthName(DateTime $date): string; + + public function getFormattedDuration(int $seconds): string; + + public function getFormattedDecimalDuration(int $seconds): string; - /** - * Returns the currency symbol for the given currency by name. - * - * @param string $currency - * @return string - */ public function getCurrencySymbol(string $currency): string; } diff --git a/src/Invoice/ServiceInvoice.php b/src/Invoice/ServiceInvoice.php index 7ba092b3..552d99cc 100644 --- a/src/Invoice/ServiceInvoice.php +++ b/src/Invoice/ServiceInvoice.php @@ -376,7 +376,7 @@ final class ServiceInvoice $this->markEntriesAsExported($model->getEntries()); } - $dispatcher->dispatch(new InvoiceCreatedEvent($invoice)); + $dispatcher->dispatch(new InvoiceCreatedEvent($invoice, $model)); return $invoice; } diff --git a/tests/Event/InvoiceCreatedEventTest.php b/tests/Event/InvoiceCreatedEventTest.php index 96548a66..efb92b45 100644 --- a/tests/Event/InvoiceCreatedEventTest.php +++ b/tests/Event/InvoiceCreatedEventTest.php @@ -11,6 +11,8 @@ namespace App\Tests\Event; use App\Entity\Invoice; use App\Event\InvoiceCreatedEvent; +use App\Tests\Invoice\DebugFormatter; +use App\Tests\Mocks\InvoiceModelFactoryFactory; use PHPUnit\Framework\TestCase; /** @@ -21,9 +23,11 @@ class InvoiceCreatedEventTest extends TestCase public function testDefaultValues() { $invoice = new Invoice(); + $model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter()); - $sut = new InvoiceCreatedEvent($invoice); + $sut = new InvoiceCreatedEvent($invoice, $model); self::assertSame($invoice, $sut->getInvoice()); + self::assertSame($model, $sut->getInvoiceModel()); } } diff --git a/tests/EventSubscriber/Actions/InvoiceSubscriberTest.php b/tests/EventSubscriber/Actions/InvoiceSubscriberTest.php new file mode 100644 index 00000000..b13e8ae2 --- /dev/null +++ b/tests/EventSubscriber/Actions/InvoiceSubscriberTest.php @@ -0,0 +1,23 @@ +assertGetSubscribedEvent(InvoiceSubscriber::class, 'invoice'); + } +} diff --git a/tests/Invoice/DebugFormatter.php b/tests/Invoice/DebugFormatter.php index e66c5bc6..3fd58623 100644 --- a/tests/Invoice/DebugFormatter.php +++ b/tests/Invoice/DebugFormatter.php @@ -13,31 +13,17 @@ use App\Invoice\InvoiceFormatter; class DebugFormatter implements InvoiceFormatter { - /** - * @param \DateTime $date - * @return mixed - */ - public function getFormattedDateTime(\DateTime $date) + public function getFormattedDateTime(\DateTime $date): string { return $date->format('d.m.Y'); } - /** - * @param \DateTime $date - * @return mixed - */ - public function getFormattedTime(\DateTime $date) + public function getFormattedTime(\DateTime $date): string { return $date->format('H:i'); } - /** - * @param int|float $amount - * @param string|null $currency - * @param bool $withCurrency - * @return string - */ - public function getFormattedMoney($amount, ?string $currency, bool $withCurrency = true) + public function getFormattedMoney(float $amount, ?string $currency, bool $withCurrency = true): string { if (null === $currency) { $withCurrency = false; @@ -50,35 +36,33 @@ class DebugFormatter implements InvoiceFormatter return (string) $amount; } - /** - * @param \DateTime $date - * @return mixed - */ - public function getFormattedMonthName(\DateTime $date) + public function getFormattedMonthName(\DateTime $date): string { return $date->format('m'); } - /** - * @param mixed $seconds - * @return mixed - */ - public function getFormattedDuration($seconds) + public function getFormattedDuration(int $seconds): string { - return $seconds; + return (string) $seconds; } - /** - * @param mixed $seconds - * @return mixed - */ - public function getFormattedDecimalDuration($seconds) + public function getFormattedDecimalDuration(int $seconds): string { - return $seconds; + return (string) $seconds; } public function getCurrencySymbol(string $currency): string { return $currency; } + + public function getLocale(): string + { + return 'en'; + } + + public function setLocale(string $locale): void + { + // does nothing + } }