added invoice model to invoice created event (#3079)
* added model to InvoiceCreatedEvent * allow to switch formatter locale
This commit is contained in:
@@ -337,9 +337,10 @@ class InvoiceCreateCommand extends Command
|
|||||||
return $invoices;
|
return $invoices;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function saveInvoicePreview(Response $response)
|
private function saveInvoicePreview(Response $response): string
|
||||||
{
|
{
|
||||||
$filename = uniqid('invoice_');
|
$filename = uniqid('invoice_');
|
||||||
|
$directory = rtrim($this->previewDirectory, '/') . '/';
|
||||||
|
|
||||||
if ($response->headers->has('Content-Disposition')) {
|
if ($response->headers->has('Content-Disposition')) {
|
||||||
$disposition = $response->headers->get('Content-Disposition');
|
$disposition = $response->headers->get('Content-Disposition');
|
||||||
@@ -360,12 +361,12 @@ class InvoiceCreateCommand extends Command
|
|||||||
|
|
||||||
if ($response instanceof BinaryFileResponse) {
|
if ($response instanceof BinaryFileResponse) {
|
||||||
$file = $response->getFile();
|
$file = $response->getFile();
|
||||||
$file->move($this->previewDirectory, $filename);
|
$file->move($directory, $filename);
|
||||||
} else {
|
} else {
|
||||||
(new Filesystem())->dumpFile($this->previewDirectory . $filename, $response->getContent());
|
(new Filesystem())->dumpFile($directory . $filename, $response->getContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->previewDirectory . $filename;
|
return $directory . $filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -10,22 +10,27 @@
|
|||||||
namespace App\Event;
|
namespace App\Event;
|
||||||
|
|
||||||
use App\Entity\Invoice;
|
use App\Entity\Invoice;
|
||||||
|
use App\Invoice\InvoiceModel;
|
||||||
use Symfony\Contracts\EventDispatcher\Event;
|
use Symfony\Contracts\EventDispatcher\Event;
|
||||||
|
|
||||||
final class InvoiceCreatedEvent extends Event
|
final class InvoiceCreatedEvent extends Event
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var Invoice
|
|
||||||
*/
|
|
||||||
private $invoice;
|
private $invoice;
|
||||||
|
private $model;
|
||||||
|
|
||||||
public function __construct(Invoice $invoice)
|
public function __construct(Invoice $invoice, InvoiceModel $model)
|
||||||
{
|
{
|
||||||
$this->invoice = $invoice;
|
$this->invoice = $invoice;
|
||||||
|
$this->model = $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInvoice(): Invoice
|
public function getInvoice(): Invoice
|
||||||
{
|
{
|
||||||
return $this->invoice;
|
return $this->invoice;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getInvoiceModel(): InvoiceModel
|
||||||
|
{
|
||||||
|
return $this->model;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,74 +14,71 @@ use App\Utils\LocaleFormatter;
|
|||||||
|
|
||||||
final class DefaultInvoiceFormatter implements InvoiceFormatter
|
final class DefaultInvoiceFormatter implements InvoiceFormatter
|
||||||
{
|
{
|
||||||
|
private $locale;
|
||||||
|
private $formats;
|
||||||
/**
|
/**
|
||||||
* @var LocaleFormatter
|
* @var LocaleFormatter|null
|
||||||
*/
|
*/
|
||||||
private $formatter;
|
private $formatter;
|
||||||
|
|
||||||
public function __construct(LanguageFormattings $formats, string $locale)
|
public function __construct(LanguageFormattings $formats, string $locale)
|
||||||
{
|
{
|
||||||
$this->formatter = new LocaleFormatter($formats, $locale);
|
$this->formats = $formats;
|
||||||
|
$this->locale = $locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function getFormatter(): LocaleFormatter
|
||||||
* @param \DateTime $date
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedDateTime(\DateTime $date)
|
|
||||||
{
|
{
|
||||||
return $this->formatter->dateShort($date);
|
if ($this->formatter === null) {
|
||||||
|
$this->formatter = new LocaleFormatter($this->formats, $this->locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->formatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getFormattedDateTime(\DateTime $date): string
|
||||||
* @param \DateTime $date
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedTime(\DateTime $date)
|
|
||||||
{
|
{
|
||||||
return $this->formatter->time($date);
|
return $this->getFormatter()->dateShort($date);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getFormattedTime(\DateTime $date): string
|
||||||
* @param \DateTime $date
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedMonthName(\DateTime $date)
|
|
||||||
{
|
{
|
||||||
return $this->formatter->monthName($date);
|
return $this->getFormatter()->time($date);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getFormattedMonthName(\DateTime $date): string
|
||||||
* @param float|int $amount
|
|
||||||
* @param string|null $currency
|
|
||||||
* @param bool $withCurrency
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getFormattedMoney($amount, ?string $currency, bool $withCurrency = true)
|
|
||||||
{
|
{
|
||||||
return $this->formatter->money($amount, $currency, $withCurrency);
|
return $this->getFormatter()->monthName($date);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getFormattedMoney(float $amount, ?string $currency, bool $withCurrency = true): string
|
||||||
* @param int $seconds
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedDuration($seconds)
|
|
||||||
{
|
{
|
||||||
return $this->formatter->duration($seconds);
|
return $this->getFormatter()->money($amount, $currency, $withCurrency);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getFormattedDuration(int $seconds): string
|
||||||
* @param int $seconds
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedDecimalDuration($seconds)
|
|
||||||
{
|
{
|
||||||
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
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,49 +16,21 @@ use DateTime;
|
|||||||
*/
|
*/
|
||||||
interface InvoiceFormatter
|
interface InvoiceFormatter
|
||||||
{
|
{
|
||||||
/**
|
public function getLocale(): string;
|
||||||
* @param DateTime $date
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedDateTime(DateTime $date);
|
|
||||||
|
|
||||||
/**
|
public function setLocale(string $locale): void;
|
||||||
* @param DateTime $date
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedTime(DateTime $date);
|
|
||||||
|
|
||||||
/**
|
public function getFormattedDateTime(DateTime $date): string;
|
||||||
* @param int|float $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;
|
||||||
* @param DateTime $date
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedMonthName(DateTime $date);
|
|
||||||
|
|
||||||
/**
|
public function getFormattedMoney(float $amount, ?string $currency, bool $withCurrency = true): string;
|
||||||
* @param int $seconds
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedDuration($seconds);
|
|
||||||
|
|
||||||
/**
|
public function getFormattedMonthName(DateTime $date): string;
|
||||||
* @param int $seconds
|
|
||||||
* @return mixed
|
public function getFormattedDuration(int $seconds): string;
|
||||||
*/
|
|
||||||
public function getFormattedDecimalDuration($seconds);
|
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;
|
public function getCurrencySymbol(string $currency): string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -376,7 +376,7 @@ final class ServiceInvoice
|
|||||||
$this->markEntriesAsExported($model->getEntries());
|
$this->markEntriesAsExported($model->getEntries());
|
||||||
}
|
}
|
||||||
|
|
||||||
$dispatcher->dispatch(new InvoiceCreatedEvent($invoice));
|
$dispatcher->dispatch(new InvoiceCreatedEvent($invoice, $model));
|
||||||
|
|
||||||
return $invoice;
|
return $invoice;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ namespace App\Tests\Event;
|
|||||||
|
|
||||||
use App\Entity\Invoice;
|
use App\Entity\Invoice;
|
||||||
use App\Event\InvoiceCreatedEvent;
|
use App\Event\InvoiceCreatedEvent;
|
||||||
|
use App\Tests\Invoice\DebugFormatter;
|
||||||
|
use App\Tests\Mocks\InvoiceModelFactoryFactory;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -21,9 +23,11 @@ class InvoiceCreatedEventTest extends TestCase
|
|||||||
public function testDefaultValues()
|
public function testDefaultValues()
|
||||||
{
|
{
|
||||||
$invoice = new Invoice();
|
$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($invoice, $sut->getInvoice());
|
||||||
|
self::assertSame($model, $sut->getInvoiceModel());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
23
tests/EventSubscriber/Actions/InvoiceSubscriberTest.php
Normal file
23
tests/EventSubscriber/Actions/InvoiceSubscriberTest.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,31 +13,17 @@ use App\Invoice\InvoiceFormatter;
|
|||||||
|
|
||||||
class DebugFormatter implements InvoiceFormatter
|
class DebugFormatter implements InvoiceFormatter
|
||||||
{
|
{
|
||||||
/**
|
public function getFormattedDateTime(\DateTime $date): string
|
||||||
* @param \DateTime $date
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedDateTime(\DateTime $date)
|
|
||||||
{
|
{
|
||||||
return $date->format('d.m.Y');
|
return $date->format('d.m.Y');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getFormattedTime(\DateTime $date): string
|
||||||
* @param \DateTime $date
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedTime(\DateTime $date)
|
|
||||||
{
|
{
|
||||||
return $date->format('H:i');
|
return $date->format('H:i');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getFormattedMoney(float $amount, ?string $currency, bool $withCurrency = true): string
|
||||||
* @param int|float $amount
|
|
||||||
* @param string|null $currency
|
|
||||||
* @param bool $withCurrency
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getFormattedMoney($amount, ?string $currency, bool $withCurrency = true)
|
|
||||||
{
|
{
|
||||||
if (null === $currency) {
|
if (null === $currency) {
|
||||||
$withCurrency = false;
|
$withCurrency = false;
|
||||||
@@ -50,35 +36,33 @@ class DebugFormatter implements InvoiceFormatter
|
|||||||
return (string) $amount;
|
return (string) $amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getFormattedMonthName(\DateTime $date): string
|
||||||
* @param \DateTime $date
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedMonthName(\DateTime $date)
|
|
||||||
{
|
{
|
||||||
return $date->format('m');
|
return $date->format('m');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getFormattedDuration(int $seconds): string
|
||||||
* @param mixed $seconds
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedDuration($seconds)
|
|
||||||
{
|
{
|
||||||
return $seconds;
|
return (string) $seconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getFormattedDecimalDuration(int $seconds): string
|
||||||
* @param mixed $seconds
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFormattedDecimalDuration($seconds)
|
|
||||||
{
|
{
|
||||||
return $seconds;
|
return (string) $seconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCurrencySymbol(string $currency): string
|
public function getCurrencySymbol(string $currency): string
|
||||||
{
|
{
|
||||||
return $currency;
|
return $currency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getLocale(): string
|
||||||
|
{
|
||||||
|
return 'en';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setLocale(string $locale): void
|
||||||
|
{
|
||||||
|
// does nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user