Skip to content

Commit

Permalink
fix: no logo scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 8, 2023
1 parent 1e55e8c commit 6f44bae
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions src/Services/QrCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace IanM\TwoFactor\Services;

use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Builder\BuilderInterface;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel;
Expand All @@ -33,39 +34,52 @@ public function __construct(protected SettingsRepositoryInterface $settings, Fac

public function generate(string $text, bool $asDataUri = false): string
{
$builder = Builder::create()
->writer(new Writer\PngWriter())
->writerOptions([])
->data($text)
->encoding(new Encoding('UTF-8'))
->errorCorrectionLevel(new ErrorCorrectionLevel\ErrorCorrectionLevelQuartile())
->size(300)
->margin(10)
->roundBlockSizeMode(new RoundBlockSizeMode\RoundBlockSizeModeMargin())
->validateResult(false)
->backgroundColor(new Color(255, 255, 255, 1));
$builder = $this->buildQrOptions($text);
$builder = $this->addLogoToBuilder($builder);

try {
if ($this->settings->get('ianm-twofactor.admin.settings.forum_logo_qr') && $this->getLogoUrl()) {
$builder
->logoPath($this->getLogoUrl())
->logoResizeToWidth($this->settings->get('ianm-twofactor.admin.settings.forum_logo_qr_width') ?? 100)
->logoPunchoutBackground(true);
}
$result = $builder->build();
}
catch (\Exception $e) {
$this->logger->error('[ianm/twofactor] Could not add logo to QR code: '.$e->getMessage());
$builder = $this->buildQrOptions($text);
$result = $builder->build();
}

$result = $builder->build();

if ($asDataUri) {
return $result->getDataUri();
}

return $result->getString();
}

protected function buildQrOptions(string $text): BuilderInterface
{
return Builder::create()
->writer(new Writer\PngWriter())
->writerOptions([])
->data($text)
->encoding(new Encoding('UTF-8'))
->errorCorrectionLevel(new ErrorCorrectionLevel\ErrorCorrectionLevelQuartile())
->size(300)
->margin(10)
->roundBlockSizeMode(new RoundBlockSizeMode\RoundBlockSizeModeMargin())
->validateResult(false)
->backgroundColor(new Color(255, 255, 255, 1));
}

protected function addLogoToBuilder(BuilderInterface $builder): BuilderInterface
{
if ($this->settings->get('ianm-twofactor.admin.settings.forum_logo_qr') && $this->getLogoUrl()) {
$builder
->logoPath($this->getLogoUrl())
->logoResizeToWidth($this->settings->get('ianm-twofactor.admin.settings.forum_logo_qr_width') ?? 100)
->logoPunchoutBackground(true);
}

return $builder;
}

protected function getLogoUrl(): ?string
{
$logoPath = $this->settings->get('ianm_twofactor_logo_path') ?? $this->settings->get('logo_path');
Expand Down

0 comments on commit 6f44bae

Please sign in to comment.