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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -376,7 +376,7 @@ final class ServiceInvoice
|
||||
$this->markEntriesAsExported($model->getEntries());
|
||||
}
|
||||
|
||||
$dispatcher->dispatch(new InvoiceCreatedEvent($invoice));
|
||||
$dispatcher->dispatch(new InvoiceCreatedEvent($invoice, $model));
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user