added invoice model to invoice created event (#3079)

* added model to InvoiceCreatedEvent
* allow to switch formatter locale
This commit is contained in:
Kevin Papst
2022-01-20 18:41:55 +01:00
committed by GitHub
parent fd4cbb43c1
commit 49bb9980b8
8 changed files with 109 additions and 123 deletions

View File

@@ -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;
}
/**

View File

@@ -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;
}
}

View File

@@ -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);
}
/**
* @param \DateTime $date
* @return mixed
*/
public function getFormattedTime(\DateTime $date)
{
return $this->formatter->time($date);
return $this->formatter;
}
/**
* @param \DateTime $date
* @return mixed
*/
public function getFormattedMonthName(\DateTime $date)
public function getFormattedDateTime(\DateTime $date): string
{
return $this->formatter->monthName($date);
return $this->getFormatter()->dateShort($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 getFormattedTime(\DateTime $date): string
{
return $this->formatter->money($amount, $currency, $withCurrency);
return $this->getFormatter()->time($date);
}
/**
* @param int $seconds
* @return mixed
*/
public function getFormattedDuration($seconds)
public function getFormattedMonthName(\DateTime $date): string
{
return $this->formatter->duration($seconds);
return $this->getFormatter()->monthName($date);
}
/**
* @param int $seconds
* @return mixed
*/
public function getFormattedDecimalDuration($seconds)
public function getFormattedMoney(float $amount, ?string $currency, bool $withCurrency = true): string
{
return $this->formatter->durationDecimal($seconds);
return $this->getFormatter()->money($amount, $currency, $withCurrency);
}
public function getFormattedDuration(int $seconds): string
{
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;
}
}

View File

@@ -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;
}

View File

@@ -376,7 +376,7 @@ final class ServiceInvoice
$this->markEntriesAsExported($model->getEntries());
}
$dispatcher->dispatch(new InvoiceCreatedEvent($invoice));
$dispatcher->dispatch(new InvoiceCreatedEvent($invoice, $model));
return $invoice;
}

View File

@@ -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());
}
}

View File

@@ -0,0 +1,23 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\EventSubscriber\Actions;
use App\EventSubscriber\Actions\InvoiceSubscriber;
/**
* @covers \App\EventSubscriber\Actions\InvoiceSubscriber
*/
class InvoiceSubscriberTest extends AbstractActionsSubscriberTest
{
public function testEventName()
{
$this->assertGetSubscribedEvent(InvoiceSubscriber::class, 'invoice');
}
}

View File

@@ -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
}
}