Skip to content

Commit

Permalink
Merge pull request #16 from petrknap/14-remove-endroidqrcode-from-spa…
Browse files Browse the repository at this point in the history
…ydqrinterface

`Endroid\QrCode` removed from `SpaydQrInterface`
  • Loading branch information
petrknap authored May 25, 2024
2 parents 0a6c559 + 79d204b commit 6d665d4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .molireali
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
authors
composer PetrKnap\\SpaydQr
dockerfile php 8.1-cli
docker-scripts petrknap/php-spaydqr
docker-scripts petrknap/php-spayd-qr
donation
github-workflow docker "composer ci-script"
github-workflow linter-docker
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"url": "https://petrknap.github.io/donate.html"
}
],
"homepage": "https://github.com/petrknap/php-spaydqr",
"homepage": "https://github.com/petrknap/php-spayd-qr",
"keywords": [
"spayd",
"qrcode",
Expand Down
29 changes: 29 additions & 0 deletions src/QrCodeWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace PetrKnap\SpaydQr;

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

enum QrCodeWriter
{
case Png;
case Svg;
case WebP;

/**
* @internal factory
*/
public function endroid(): WriterInterface
{
return match ($this) {
self::Png => new PngWriter(),
self::Svg => new SvgWriter(),
self::WebP => new WebPWriter(),
};
}
}
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 6d665d4

Please sign in to comment.