Skip to content

Commit

Permalink
Separate FontAwesome services. Added default text value to PdfColumn.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Oct 28, 2024
1 parent 1a4b1c0 commit 99093e4
Show file tree
Hide file tree
Showing 23 changed files with 246 additions and 218 deletions.
2 changes: 1 addition & 1 deletion config/packages/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
->adapters('cache.adapter.filesystem')
->defaultLifetime($one_day);

// FontAwesomeService
// FontAwesomeImageService
$config->cache()->pool('calculation.fontawesome')
->adapters('cache.adapter.filesystem')
->defaultLifetime($one_month);
Expand Down
6 changes: 3 additions & 3 deletions src/Command/FontAwesomeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace App\Command;

use App\Service\FontAwesomeService;
use App\Service\FontAwesomeImageService;
use App\Utils\FileUtils;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -129,7 +129,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$relativeTarget = $this->getRelativePath($target);

\ksort($aliases);
$aliasesPath = FileUtils::buildPath($tempDir, FontAwesomeService::ALIAS_FILE_NAME);
$aliasesPath = FileUtils::buildPath($tempDir, FontAwesomeImageService::ALIAS_FILE_NAME);
if (!FileUtils::dumpFile($aliasesPath, (string) \json_encode($aliases, \JSON_PRETTY_PRINT))) {
$io->error(\sprintf('Unable to copy aliases file to the directory: "%s".', $relativeTarget));

Expand Down Expand Up @@ -224,7 +224,7 @@ private function getSourceFile(SymfonyStyle $io): string

private function getSvgFileName(string $style, string|int $name): string
{
return \sprintf('%s/%s%s', $style, $name, FontAwesomeService::SVG_EXTENSION);
return \sprintf('%s/%s%s', $style, $name, FontAwesomeImageService::SVG_EXTENSION);
}

private function getTargetDirectory(SymfonyStyle $io): string
Expand Down
12 changes: 8 additions & 4 deletions src/Controller/LogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
use App\Model\LogFile;
use App\Report\LogsReport;
use App\Resolver\DataQueryValueResolver;
use App\Service\FontAwesomeService;
use App\Service\FontAwesomeIconService;
use App\Service\FontAwesomeImageService;
use App\Service\LogService;
use App\Spreadsheet\LogsDocument;
use App\Table\DataQuery;
Expand Down Expand Up @@ -129,13 +130,16 @@ public function index(
* Export to PDF the content of the log file.
*/
#[Get(path: '/pdf', name: 'pdf')]
public function pdf(LogService $logService, FontAwesomeService $service): Response
{
public function pdf(
LogService $logService,
FontAwesomeImageService $imageService,
FontAwesomeIconService $iconService
): Response {
$logFile = $this->getLogFile($logService);
if (!$logFile instanceof LogFile) {
return $this->getEmptyResponse();
}
$doc = new LogsReport($this, $logFile, $service);
$doc = new LogsReport($this, $logFile, $imageService, $iconService);

return $this->renderPdfDocument($doc);
}
Expand Down
17 changes: 13 additions & 4 deletions src/Controller/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
use App\Response\WordResponse;
use App\Service\AbstractHttpClientService;
use App\Service\CaptchaImageService;
use App\Service\FontAwesomeService;
use App\Service\FontAwesomeIconService;
use App\Service\FontAwesomeImageService;
use App\Service\MailerService;
use App\Service\PdfLabelService;
use App\Service\RecaptchaResponseService;
Expand Down Expand Up @@ -244,7 +245,7 @@ public function exportWord(): WordResponse
* Output a report with Fontawesome images.
*/
#[Get(path: '/fontawesome', name: 'fontawesome')]
public function fontAwesome(FontAwesomeService $service): Response
public function fontAwesome(FontAwesomeImageService $service): Response
{
if (!$service->isSvgSupported() || $service->isImagickException()) {
return $this->redirectToHomePage(
Expand Down Expand Up @@ -280,9 +281,17 @@ public function memoryImage(
string $iconFile,
#[Autowire('%kernel.project_dir%/public/images/screenshots/home_light.png')]
string $screenshotFile,
FontAwesomeService $service
FontAwesomeImageService $imageService,
FontAwesomeIconService $iconService
): PdfResponse {
$report = new MemoryImageReport($this, $logoFile, $iconFile, $screenshotFile, $service);
$report = new MemoryImageReport(
$this,
$logoFile,
$iconFile,
$screenshotFile,
$imageService,
$iconService
);

return $this->renderPdfDocument($report);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Pdf/PdfColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PdfColumn
* This property is used only if the parent's table uses all the document width.
*/
public function __construct(
private ?string $text,
private ?string $text = null,
private float $width = 0.0,
private ?PdfTextAlignment $alignment = PdfTextAlignment::LEFT,
private bool $fixed = false
Expand All @@ -44,7 +44,7 @@ public function __construct(
*
* @return PdfColumn the new newly created column
*/
public static function center(?string $text, float $width, bool $fixed = false): self
public static function center(?string $text = null, float $width = 0.0, bool $fixed = false): self
{
return new self($text, $width, PdfTextAlignment::CENTER, $fixed);
}
Expand Down Expand Up @@ -97,7 +97,7 @@ public function isFixed(): bool
*
* @return PdfColumn the new newly created column
*/
public static function left(?string $text, float $width, bool $fixed = false): self
public static function left(?string $text = null, float $width = 0.0, bool $fixed = false): self
{
return new self($text, $width, PdfTextAlignment::LEFT, $fixed);
}
Expand All @@ -112,7 +112,7 @@ public static function left(?string $text, float $width, bool $fixed = false): s
*
* @return PdfColumn the new newly created column
*/
public static function right(?string $text, float $width, bool $fixed = false): self
public static function right(?string $text = null, float $width = 0.0, bool $fixed = false): self
{
return new self($text, $width, PdfTextAlignment::RIGHT, $fixed);
}
Expand Down
11 changes: 7 additions & 4 deletions src/Report/CalculationReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\Writer\PngWriter;
use fpdf\Enums\PdfMove;
use fpdf\Enums\PdfTextAlignment;
use fpdf\PdfBorder;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -84,7 +85,7 @@ public function render(): bool
$this->renderTitle($calculation);
$this->addPage();
if ($calculation->isEmpty()) {
return $this->renderEmpty();
return $this->renderEmpty($calculation);
}

ItemsTable::render($this);
Expand Down Expand Up @@ -180,18 +181,20 @@ private function renderCalculation(): void
/**
* Render a text when the calculation is empty.
*/
private function renderEmpty(): true
private function renderEmpty(Calculation $calculation): true
{
PdfStyle::getCellStyle()
PdfStyle::getHeaderStyle()
->setTextColor(PdfTextColor::red())
->apply($this);
$this->cell(
height: self::LINE_HEIGHT * 2.0,
height: self::LINE_HEIGHT * 1.25,
text: $this->trans('calculation.edit.empty'),
border: PdfBorder::all(),
move: PdfMove::BELOW,
align: PdfTextAlignment::CENTER,
fill: true
);
$this->renderTimestampable($calculation);

return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Report/FontAwesomeReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use App\Pdf\PdfGroupTable;
use App\Pdf\PdfStyle;
use App\Pdf\Traits\PdfMemoryImageTrait;
use App\Service\FontAwesomeService;
use App\Service\FontAwesomeImageService;
use fpdf\Enums\PdfTextAlignment;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
Expand All @@ -30,7 +30,7 @@ class FontAwesomeReport extends AbstractReport

public function __construct(
AbstractController $controller,
private readonly FontAwesomeService $service
private readonly FontAwesomeImageService $service
) {
parent::__construct($controller);
$this->setTitle('Font Awesome Icons');
Expand All @@ -48,7 +48,7 @@ public function render(): bool
->setGroupStyle(PdfStyle::getHeaderStyle());
$width = $this->getPrintableWidth() / (float) $columns;
for ($i = 0; $i < $columns; ++$i) {
$table->addColumn(PdfColumn::left('', $width));
$table->addColumn(PdfColumn::left(width: $width));
}
$total = $this->renderIcons($table, $columns) + $this->renderAliases($table, $columns);
$this->renderTotal($total);
Expand Down Expand Up @@ -155,7 +155,7 @@ private function renderIcons(PdfGroupTable $table, int $columns): int
$index = 0;
$finder = Finder::create()
->in($this->service->getSvgDirectory())
->name('*' . FontAwesomeService::SVG_EXTENSION)
->name('*' . FontAwesomeImageService::SVG_EXTENSION)
->files();

foreach ($finder as $file) {
Expand Down
Loading

0 comments on commit 99093e4

Please sign in to comment.