Skip to content

Commit

Permalink
refactor: interface isolated from underlying libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
petrknap committed May 25, 2024
1 parent 0a6c559 commit fbf0f9b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
26 changes: 26 additions & 0 deletions src/QrCodeWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace PetrKnap\SpaydQr;

use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\SvgWriter;
use Endroid\QrCode\Writer\WriterInterface;

enum QrCodeWriter
{
case Png;
case Svg;

/**
* @internal factory
*/
public function endroid(): WriterInterface
{
return match ($this) {
self::Png => new PngWriter(),
self::Svg => new SvgWriter(),
};
}
}
11 changes: 5 additions & 6 deletions src/SpaydQr.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Builder\BuilderInterface;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\Result\ResultInterface;
use Endroid\QrCode\Writer\WriterInterface;
use Money\Currencies\ISOCurrencies;
use Money\Formatter\DecimalMoneyFormatter;
use Money\Money;
use Sunfox\Spayd\Spayd;

/**
* @todo make it final
* @todo create SpaydBuilder
*/
class SpaydQr implements SpaydQrInterface
{
Expand All @@ -35,12 +34,12 @@ protected function __construct(
->add(self::SPAYD_CURRENCY, $money->getCurrency()->getCode());
}

public static function create(string $iban, Money $money, WriterInterface $writer = null): self
public static function create(string $iban, Money $money, QrCodeWriter $writer = QrCodeWriter::Png): self
{
return new self(
new Spayd(),
Builder::create()
->writer($writer ?: new PngWriter())
->writer($writer->endroid())
->encoding(new Encoding('UTF-8')),
$iban,
$money
Expand Down Expand Up @@ -87,9 +86,9 @@ public function setInvoice(
return $this;
}

public function setWriter(WriterInterface $writer): self
public function setWriter(QrCodeWriter $writer): self
{
$this->qrCodeBuilder->writer($writer);
$this->qrCodeBuilder->writer($writer->endroid());

return $this;
}
Expand Down
6 changes: 1 addition & 5 deletions src/SpaydQrInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace PetrKnap\SpaydQr;

use Endroid\QrCode\Writer\WriterInterface;
use Money\Money;

interface SpaydQrInterface
Expand Down Expand Up @@ -44,10 +43,7 @@ public function setInvoice(
?string $description
): self;

/**
* @todo create own enum of writers
*/
public function setWriter(WriterInterface $writer): self;
public function setWriter(QrCodeWriter $writer): self;

public function getContentType(): string;

Expand Down
6 changes: 3 additions & 3 deletions tests/SpaydQrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public function testFactoryWorks()

public function testSetWriterWorks()
{
$writer = $this->getMockBuilder(WriterInterface::class)->getMock();
$writer = QrCodeWriter::Svg;
$qrCodeBuilder = $this->getMockBuilder(BuilderInterface::class)->getMock();
$qrCodeBuilder->expects($this->once())
->method('writer')
->with($writer)
->with($writer->endroid())
->willReturnSelf();

$this->getSpaydQr(null, $qrCodeBuilder)->setWriter($writer);
Expand Down Expand Up @@ -245,7 +245,7 @@ public function dataWriteFileWorks()
public function testEndToEnd()
{
$spaydQr = $this->getSpaydQr(null, null)
->setWriter(new SvgWriter())
->setWriter(QrCodeWriter::Svg)
->setVariableSymbol(123)
->setInvoice(
'1',
Expand Down

0 comments on commit fbf0f9b

Please sign in to comment.