Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Invoice path relative to invoices_root_dir. Added invoice naming method. #5

Merged
merged 6 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
!/etc/build/.gitkeep

/tests/Application/yarn.lock
/.idea
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove it from the .gitignore. Configure a global .gitignore file instead. Read more here: https://gist.github.com/subfuzion/db7f57fff2fb6998a16c

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ theme path, in case you are using multiple themes.
bitbag_sylius_invoicing_plugin.event_listener.order_show BitBag\SyliusInvoicingPlugin\Menu\DownloadInvoiceMenuBuilder
bitbag_sylius_invoicing_plugin.factory.company_data Sylius\Component\Resource\Factory\Factory
bitbag_sylius_invoicing_plugin.factory.invoice Sylius\Component\Resource\Factory\Factory
bitbag_sylius_invoicing_plugin.file_generator.invoice_filename BitBag\SyliusInvoicingPlugin\FileGenerator\InvoicePdfFilenameGenerator
bitbag_sylius_invoicing_plugin.file_generator.invoice_file BitBag\SyliusInvoicingPlugin\FileGenerator\InvoicePdfFileGenerator
bitbag_sylius_invoicing_plugin.form.extension.address BitBag\SyliusInvoicingPlugin\Form\Extension\AddressTypeExtension
bitbag_sylius_invoicing_plugin.form.type.company_data BitBag\SyliusInvoicingPlugin\Form\Type\CompanyDataType
Expand Down
2 changes: 1 addition & 1 deletion src/FileGenerator/FileGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ public function generateFile(InvoiceInterface $invoice): string;
/**
* @return string
*/
public function getFilesPath(): string;
public function getFilesDirectoryPath(): string;
}
25 changes: 25 additions & 0 deletions src/FileGenerator/FilenameGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on mikolaj.krol@bitbag.pl.
*/

declare(strict_types=1);

namespace BitBag\SyliusInvoicingPlugin\FileGenerator;

use BitBag\SyliusInvoicingPlugin\Entity\InvoiceInterface;

interface FilenameGeneratorInterface
{
/**
* @param InvoiceInterface $invoice
*
* @return string
*/
public function generateFilename(InvoiceInterface $invoice): string;
}
29 changes: 11 additions & 18 deletions src/FileGenerator/InvoicePdfFileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

final class InvoicePdfFileGenerator implements FileGeneratorInterface
{
/**
* @var FilenameGeneratorInterface
*/
protected $filenameGenerator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, mark it as private, the class is final, so there's no need to add protected fields to it.


/**
* @var GeneratorInterface
*/
Expand All @@ -44,17 +49,20 @@ final class InvoicePdfFileGenerator implements FileGeneratorInterface
* @param EngineInterface $templatingEngine
* @param CompanyDataResolverInterface $companyDataResolver
* @param string $filesPath
* @param FilenameGeneratorInterface $filenameGenerator
*/
public function __construct(
GeneratorInterface $pdfFileGenerator,
EngineInterface $templatingEngine,
CompanyDataResolverInterface $companyDataResolver,
string $filesPath
string $filesPath,
FilenameGeneratorInterface $filenameGenerator
) {
$this->pdfFileGenerator = $pdfFileGenerator;
$this->templatingEngine = $templatingEngine;
$this->companyDataResolver = $companyDataResolver;
$this->filesPath = $filesPath;
$this->filenameGenerator = $filenameGenerator;
}

/**
Expand All @@ -68,33 +76,18 @@ public function generateFile(InvoiceInterface $invoice): string
'companyData' => $this->companyDataResolver->resolveCompanyData(),
]
);
$filename = $this->getInvoiceFilename($invoice);
$filename = $this->filenameGenerator->generateFilename($invoice);
$path = $this->filesPath . DIRECTORY_SEPARATOR . $filename;

$this->pdfFileGenerator->generateFromHtml($html, $path);

return $filename;
}

/**
* @param InvoiceInterface $invoice
* @return string Returns an explicit invoice file name
*/
protected function getInvoiceFilename(InvoiceInterface $invoice): string
{
$tokens = [
$invoice->getOrder()->getNumber(),
$invoice->getOrder()->getCreatedAt()->format('Ymd'),
bin2hex(random_bytes(6)),
];

return (string) implode('_', $tokens) . '.pdf';
}

/**
* {@inheritdoc}
*/
public function getFilesPath(): string
public function getFilesDirectoryPath(): string
{
return $this->filesPath;
}
Expand Down
32 changes: 32 additions & 0 deletions src/FileGenerator/InvoicePdfFilenameGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on mikolaj.krol@bitbag.pl.
*/

declare(strict_types=1);

namespace BitBag\SyliusInvoicingPlugin\FileGenerator;

use BitBag\SyliusInvoicingPlugin\Entity\InvoiceInterface;

final class InvoicePdfFilenameGenerator implements FilenameGeneratorInterface
{
/**
* @inheritDoc
*/
public function generateFilename(InvoiceInterface $invoice): string
{
$tokens = [
$invoice->getOrder()->getNumber(),
$invoice->getOrder()->getCreatedAt()->format('Ymd'),
bin2hex(random_bytes(6)),
];

return (string) implode('_', $tokens) . '.pdf';
}
}
2 changes: 1 addition & 1 deletion src/Resolver/InvoiceFileResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ public function resolveInvoicePath(InvoiceInterface $invoice): string
$this->invoiceEntityManager->flush();
}

return $this->invoiceFileGenerator->getFilesPath() . DIRECTORY_SEPARATOR . $invoice->getPath();
return $this->invoiceFileGenerator->getFilesDirectoryPath() . DIRECTORY_SEPARATOR . $invoice->getPath();
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ imports:
- { resource: "@BitBagSyliusInvoicingPlugin/Resources/config/services/resolver.yml" }

services:
bitbag_sylius_invoicing_plugin.file_generator.invoice_filename:
class: BitBag\SyliusInvoicingPlugin\FileGenerator\InvoicePdfFilenameGenerator

bitbag_sylius_invoicing_plugin.file_generator.invoice_file:
class: BitBag\SyliusInvoicingPlugin\FileGenerator\InvoicePdfFileGenerator
arguments:
- "@knp_snappy.pdf"
- "@templating"
- "@bitbag_sylius_invoicing_plugin.resolver.company_data"
- "%invoices_root_dir%"
- "@bitbag_sylius_invoicing_plugin.file_generator.invoice_filename"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our convention, we put the object types above the default types, so if possible, interfaces over strings, arrays, etc. Could you please change this order remembering the right order in __construct call as well as properties order in InvoicePdfFileGenerator? 🙂


bitbag_sylius_invoicing_plugin.validator.vat_number:
class: BitBag\SyliusInvoicingPlugin\Validator\Constraints\VatNumberValidator
Expand Down