Skip to content

Commit

Permalink
added abstract isUsable methode to AbstractPdfCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Feb 26, 2021
1 parent afab64e commit e04dcad
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.

## [0.2.0] - 2021-02-26
- added abstract isUsable methode to AbstractPdfCreator
- added MissingDependenciesException
- made type property protected in PdfCreatorFactory

## [0.1.0] - 2021-02-26

Initial release
Expand Down
8 changes: 8 additions & 0 deletions PdfCreator/AbstractPdfCreator.php
Expand Up @@ -8,6 +8,7 @@

namespace HeimrichHannot\PdfCreator;

use HeimrichHannot\PdfCreator\Exception\MissingDependenciesException;
use Psr\Log\LoggerInterface;

abstract class AbstractPdfCreator
Expand Down Expand Up @@ -72,6 +73,13 @@ abstract class AbstractPdfCreator
*/
abstract public static function getType(): string;

/**
* Check if all prerequisites for this bundle are fullfilled, typically check for installed libraries.
*
* @throws MissingDependenciesException
*/
abstract public static function isUsable(bool $triggerExeption = false): bool;

/**
* @return mixed
*/
Expand Down
22 changes: 16 additions & 6 deletions PdfCreator/Concrete/MpdfCreator.php
Expand Up @@ -12,6 +12,7 @@
use HeimrichHannot\PdfCreator\AbstractPdfCreator;
use HeimrichHannot\PdfCreator\BeforeCreateLibraryInstanceCallback;
use HeimrichHannot\PdfCreator\BeforeOutputPdfCallback;
use HeimrichHannot\PdfCreator\Exception\MissingDependenciesException;
use Mpdf\Config\ConfigVariables;
use Mpdf\Config\FontVariables;
use Mpdf\Mpdf;
Expand All @@ -24,22 +25,31 @@ class MpdfCreator extends AbstractPdfCreator
*/
protected $legacyFontDirectoryConfig;

/**
* MpdfCreator constructor.
*/
public function __construct()
public static function isUsable(bool $triggerExeption = false): bool
{
if (!class_exists('Mpdf\Mpdf')) {
throw new \Exception('The mPDF library could not be found and is required by this service. Please install it with "composer require mpdf/mpdf ^8.0".');
if ($triggerExeption) {
throw new MissingDependenciesException(static::getType(), ['"mpdf/mpdf": "^8.0"']);
}

return false;
}

if (version_compare(Mpdf::VERSION, '7.0') < 0 || version_compare(Mpdf::VERSION, 9) >= 0) {
throw new \Exception('Only mPDF library versions 7.x and 8.x are supported.');
if ($triggerExeption) {
throw new MissingDependenciesException(static::getType(), ['"mpdf/mpdf": "^7.0|^8.0"']);
}

return false;
}

return true;
}

public function render(): void
{
static::isUsable(true);

$config = [];

if ($this->getMediaType()) {
Expand Down
24 changes: 15 additions & 9 deletions PdfCreator/Concrete/TcpdfCreator.php
Expand Up @@ -11,28 +11,34 @@
use HeimrichHannot\PdfCreator\AbstractPdfCreator;
use HeimrichHannot\PdfCreator\BeforeCreateLibraryInstanceCallback;
use HeimrichHannot\PdfCreator\BeforeOutputPdfCallback;
use HeimrichHannot\PdfCreator\Exception\MissingDependenciesException;
use setasign\Fpdi\Tcpdf\Fpdi;
use TCPDF;

class TcpdfCreator extends AbstractPdfCreator
{
/**
* TcpdfCreator constructor.
*/
public function __construct()
public static function getType(): string
{
if (!class_exists('TCPDF')) {
throw new \Exception('The TCPDF library could not be found and is required by this service. Please install it with "composer require tecnickcom/tcpdf ^6.3".');
}
return 'tcpdf';
}

public static function getType(): string
public static function isUsable(bool $triggerExeption = false): bool
{
return 'tcpdf';
if (!class_exists('TCPDF')) {
if ($triggerExeption) {
throw new MissingDependenciesException(static::getType(), ['"tecnickcom/tcpdf": "^6.3"']);
}

return false;
}

return true;
}

public function render(): void
{
static::isUsable(true);

$orientation = '';

if ($this->getOrientation()) {
Expand Down
52 changes: 52 additions & 0 deletions PdfCreator/Exception/MissingDependenciesException.php
@@ -0,0 +1,52 @@
<?php

/*
* Copyright (c) 2021 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\PdfCreator\Exception;

use Throwable;

class MissingDependenciesException extends \Exception
{
/**
* @var string
*/
protected $type;
/**
* @var array
*/
protected $dependencies;

public function __construct(string $type, array $dependencies = [], $message = '', $code = 0, Throwable $previous = null)
{
if (empty($message)) {
$message = 'There are missing dependencies the %type% pdf creator type needs to work. %dependencies%';
}

$message = str_replace('%type%', $type, $message);

if (!empty($dependencies)) {
$message = str_replace('%dependencies%', "\nMissing dependencies:\n".implode("\n", $dependencies), $message);
} else {
$message = trim(str_replace('%dependencies%', '', $message));
}

parent::__construct($message, $code, $previous);
$this->type = $type;
$this->dependencies = $dependencies;
}

public function getType(): string
{
return $this->type;
}

public function getDependencies(): array
{
return $this->dependencies;
}
}
2 changes: 1 addition & 1 deletion PdfCreator/PdfCreatorFactory.php
Expand Up @@ -13,7 +13,7 @@

class PdfCreatorFactory
{
public static $types;
protected static $types;

/**
* Return supported pdf creator types.
Expand Down

0 comments on commit e04dcad

Please sign in to comment.