Skip to content

Commit

Permalink
Version 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
eclipxe13 committed Aug 26, 2019
2 parents a35e81a + 73b44c9 commit 521884a
Show file tree
Hide file tree
Showing 32 changed files with 723 additions and 283 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ script:
- vendor/bin/php-cs-fixer fix --using-cache=no --dry-run --verbose
- |
if [[ $TRAVIS_PHP_VERSION == $FULL_BUILD_PHP_VERSION ]]; then
phpenv config-add xdebug.ini
vendor/bin/phpunit --coverage-text --coverage-clover=build/tests/coverage.xml
php -d zend_extension=xdebug.so vendor/bin/phpunit --coverage-text --coverage-clover=build/tests/coverage.xml
else
vendor/bin/phpunit
fi
Expand Down
4 changes: 2 additions & 2 deletions bin/cfditopdf
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exit(call_user_func(function (array $arguments): int {
$versionFile = __DIR__ . '/version.txt';
$version = '';
if (is_readable($versionFile)) {
$version = trim((string) file_get_contents($versionFile));
$version = trim(strval(file_get_contents($versionFile)));
}
if ('' === $version) {
$version = 'development';
Expand Down Expand Up @@ -72,7 +72,7 @@ exit(call_user_func(function (array $arguments): int {
return 0;
}

$script = new PhpCfdi\CfdiToPdf\Script\ConvertScript();
$script = new \PhpCfdi\CfdiToPdf\Script\ConvertScript();
$script->run($options);
echo $options->outputFile() . PHP_EOL;

Expand Down
10 changes: 6 additions & 4 deletions build-phar
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#!/bin/bash -e

cd "$(dirname "$0")"

PHAR_BUILDER_LOCATION="https://github.com/MacFJA/PharBuilder/releases/download/0.2.8/phar-builder.phar"
PHAR_BUILDER_LOCATION="https://github.com/MacFJA/PharBuilder/releases/latest/download/phar-builder.phar"
PHAR_BUILDER_BIN="tools/phar-builder"

# this is set in composer.json
PHAR_DESTINATION="build/cfditopdf.phar"

if [ ! -e "$PHAR_BUILDER_BIN" ]; then
mkdir -p "$(dirname "$PHAR_BUILDER_BIN")"
wget -O "$PHAR_BUILDER_BIN" "$PHAR_BUILDER_LOCATION"
fi

git describe --tags > bin/version.txt
rm -f build/cfditopdf.phar
mkdir -p "$(dirname "$PHAR_DESTINATION")"
rm -f "$PHAR_DESTINATION"
php -dphar.readonly=0 tools/phar-builder package --no-interaction
rm bin/version.txt
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"php": ">=7.0",
"eclipxe/cfdiutils": "^2.4.3",
"eclipxe/cfdiutils": "^2.11.0",
"league/plates": "^3.3",
"spipu/html2pdf": "^5.1"
},
Expand Down
13 changes: 13 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Version 0.3.0 2019-08-26

- Fix bug on `CfdiDataBuilder::createTfdSourceString` when *TimbreFiscalDigital* is version 1.0.
- Extract logic from `Html2PdfBuilder::build` to:
- `Html2PdfBuilder::buildPdf`: convert from CfdiData to html to pdf
- `Html2PdfBuilder::convertHtmlToPdf`: contains the logic of Html2Pdf
- Template `generic.php` uses alternative syntax for control structures
- Removed:
- `PhpCfdi\CfdiToPdf\Utils\TemporaryFilename`: using `CfdiUtils`
- `PhpCfdi\CfdiToPdf\Utils\ShellExec`: using `CfdiUtils`
- `PhpCfdi\CfdiToPdf\PdfToText`: used only on testing environment
- Improve test coverage

# Version 0.2.3 2019-08-15

- Due GitHub API change, need to upgrade deploy section on Travis and tag a new release
Expand Down
8 changes: 3 additions & 5 deletions docs/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
- [ ] Catálogos para expresar las claves
- [ ] Impresión genérica de complementos
- [ ] Crear nuevos métodos para generar el archivo PDF
- [ ] Depend on new version of CfdiUtils
- [ ] Move PdfToText to a different library and require it on development

- [ ] Improve testing

- [X] Depend on new version of CfdiUtils
- [X] Move PdfToText to a different library and require it only on development
- [X] Improve testing
- [X] When macfja/phar-builder reaches phive move it from composer script requiere run remove
to phive dependence

3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
inferPrivatePropertyTypeFromConstructor: true

10 changes: 9 additions & 1 deletion src/Builders/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@

interface BuilderInterface
{
public function build(CfdiData $node, string $destination);
/**
* Transform CfdiData contents to a PDF file
* and store its contents on $destination
*
* @param CfdiData $data
* @param string $destination
* @return void
*/
public function build(CfdiData $data, string $destination);
}
38 changes: 32 additions & 6 deletions src/Builders/Html2PdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,47 @@

namespace PhpCfdi\CfdiToPdf\Builders;

use \Spipu\Html2Pdf\Html2Pdf as Html2Pdf;
use League\Plates\Engine as PlatesEngine;
use PhpCfdi\CfdiToPdf\CfdiData;
use RuntimeException;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Html2Pdf;

class Html2PdfBuilder implements BuilderInterface
{
public function build(CfdiData $data, string $destination)
{
$html2Pdf = new Html2Pdf('P', 'Letter', 'es', true, 'UTF-8', [10, 10, 10, 10]);
$html2Pdf->writeHTML($this->convertNodeToHtml($data));
// don't do it directly since output method check that the file extension is pdf
file_put_contents($destination, $html2Pdf->output('null.pdf', 'S'));
file_put_contents($destination, $this->buildPdf($data));
}

public function convertNodeToHtml(CfdiData $cfdiData)
/**
* Transforms CfdiData to Pdf string
*
* @param CfdiData $data
* @return string
*/
public function buildPdf(CfdiData $data): string
{
$html = $this->convertNodeToHtml($data);
$output = $this->convertHtmlToPdf($html);
return $output;
}

public function convertHtmlToPdf(string $html): string
{
// don't do it directly since Html2Pdf::output check that the file extension is pdf
try {
$html2Pdf = new Html2Pdf('P', 'Letter', 'es', true, 'UTF-8', [10, 10, 10, 10]);
$html2Pdf->writeHTML($html);
$output = $html2Pdf->output('', 'S');
return $output;
} catch (Html2PdfException $exception) {
/** @codeCoverageIgnore don't know how to invoke this exception on Html2Pdf */
throw new RuntimeException('Unable to convert CFDI', 0, $exception);
}
}

public function convertNodeToHtml(CfdiData $cfdiData): string
{
// __DIR__ is src/Builders
$plates = new PlatesEngine(dirname(__DIR__, 2) . '/templates/');
Expand Down
7 changes: 4 additions & 3 deletions src/CfdiData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpCfdi\CfdiToPdf;

use CfdiUtils\Nodes\NodeInterface;
use RuntimeException;

class CfdiData
{
Expand All @@ -30,15 +31,15 @@ public function __construct(NodeInterface $comprobante, string $qrUrl, string $t
{
$emisor = $comprobante->searchNode('cfdi:Emisor');
if (null === $emisor) {
throw new \RuntimeException('El CFDI no contiene nodo emisor');
throw new RuntimeException('El CFDI no contiene nodo emisor');
}
$receptor = $comprobante->searchNode('cfdi:Receptor');
if (null === $receptor) {
throw new \RuntimeException('El CFDI no contiene nodo receptor');
throw new RuntimeException('El CFDI no contiene nodo receptor');
}
$timbreFiscalDigital = $comprobante->searchNode('cfdi:Complemento', 'tfd:TimbreFiscalDigital');
if (null === $timbreFiscalDigital) {
throw new \RuntimeException('El CFDI no contiene complemento de timbre fiscal digital');
throw new RuntimeException('El CFDI no contiene complemento de timbre fiscal digital');
}

$this->comprobante = $comprobante;
Expand Down
2 changes: 1 addition & 1 deletion src/CfdiDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function createTfdSourceString(NodeInterface $comprobante): string
return '';
}
$tfdCadenaOrigen = new TfdCadenaDeOrigen($this->xmlResolver(), $this->xsltBuilder());
return $tfdCadenaOrigen->build(XmlNodeUtils::nodeToXmlString($tfd), $tfd['Version'] ?? $tfd['Version'] ?? '0');
return $tfdCadenaOrigen->build(XmlNodeUtils::nodeToXmlString($tfd));
}

public function createQrUrl(NodeInterface $comprobante): string
Expand Down
17 changes: 8 additions & 9 deletions src/Converter.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

/** @noinspection PhpInternalEntityUsedInspection */

declare(strict_types=1);

namespace PhpCfdi\CfdiToPdf;

use CfdiUtils\Internals\TemporaryFile;
use PhpCfdi\CfdiToPdf\Builders\BuilderInterface;

class Converter
Expand All @@ -18,18 +21,14 @@ public function __construct(BuilderInterface $builder)

public function createPdfAs(CfdiData $cfdiData, string $destination)
{
$temporary = $this->createPdf($cfdiData);
copy($temporary, $destination);
unlink($temporary);
$this->builder->build($cfdiData, $destination);
}

public function createPdf(CfdiData $cfdiData): string
{
$temporary = new Utils\TemporaryFilename();

$this->builder->build($cfdiData, $temporary->filename());

$temporary->setDeleteOnDestruct(false);
return $temporary->filename();
$temporary = TemporaryFile::create();
$filename = $temporary->getPath();
$this->builder->build($cfdiData, $filename);
return $filename;
}
}
8 changes: 5 additions & 3 deletions src/Script/ConvertOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PhpCfdi\CfdiToPdf\Script;

use RuntimeException;

class ConvertOptions
{
/** @var string */
Expand Down Expand Up @@ -32,7 +34,7 @@ public function __construct(
bool $askForHelp,
bool $askForVersion
) {
if ('' === $outputFile) {
if ('' === $outputFile && '' !== $inputFile) {
$outputFile = (string) preg_replace('/\.xml$/', '', $inputFile) . '.pdf';
}

Expand Down Expand Up @@ -106,7 +108,7 @@ public static function createFromArguments(array $arguments): self
if (in_array($argument, ['-l', '--resource-location'], true)) {
$i = $i + 1;
if (! ($i < $count)) {
throw new \RuntimeException('The resource location parameter does not contains an argument');
throw new RuntimeException('The resource location parameter does not contains an argument');
}
$resolverLocation = $arguments[$i];
continue;
Expand All @@ -119,7 +121,7 @@ public static function createFromArguments(array $arguments): self
$outputFile = $argument;
continue;
}
throw new \RuntimeException("Unexpected parameter '$argument'");
throw new RuntimeException("Unexpected parameter '$argument'");
}

return new self($resolverLocation, $cleanInput, $inputFile, $outputFile, $askForHelp, $askForVersion);
Expand Down
15 changes: 7 additions & 8 deletions src/Script/ConvertScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpCfdi\CfdiToPdf\Builders\Html2PdfBuilder;
use PhpCfdi\CfdiToPdf\CfdiDataBuilder;
use PhpCfdi\CfdiToPdf\Converter;
use RuntimeException;

class ConvertScript
{
Expand All @@ -29,22 +30,20 @@ public function run(ConvertOptions $options)
public function openSource(string $inputfile, bool $doCleanInput): string
{
if ('' === $inputfile) {
throw new \RuntimeException('Did not provide an input file');
throw new RuntimeException('Did not provide an input file');
}
$filename = (string) realpath($inputfile);
if ('' === $filename) {
throw new \RuntimeException("The file $inputfile does not exists");
throw new RuntimeException("The file $inputfile does not exists");
}
if (! is_file($filename)) {
throw new \RuntimeException("The path $inputfile is not a file");
}
if (! is_readable($filename)) {
throw new \RuntimeException("The file $inputfile is not readable");
throw new RuntimeException("The path $inputfile is not a file");
}

$source = (string) file_get_contents($filename);
/** @noinspection PhpUsageOfSilenceOperatorInspection */
$source = strval(@file_get_contents($filename));
if ('' === $source) {
throw new \RuntimeException("The file $inputfile is empty");
throw new RuntimeException("The file $inputfile is empty");
}
if ($doCleanInput) {
$source = $this->cleanSource($source);
Expand Down
73 changes: 0 additions & 73 deletions src/Utils/ShellExec.php

This file was deleted.

Loading

0 comments on commit 521884a

Please sign in to comment.