Skip to content

Commit c3f3c0a

Browse files
committed
Move logo implementation to its own class
1 parent 2060811 commit c3f3c0a

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

lib/Providers/Qr/EndroidQrCodeProvider.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ class EndroidQrCodeProvider implements IQRCodeProvider
1010
public $color;
1111
public $margin;
1212
public $errorcorrectionlevel;
13-
protected $logoPath;
14-
protected $logoSize;
1513

1614
public function __construct($bgcolor = 'ffffff', $color = '000000', $margin = 0, $errorcorrectionlevel = 'H')
1715
{
@@ -21,23 +19,17 @@ public function __construct($bgcolor = 'ffffff', $color = '000000', $margin = 0,
2119
$this->errorcorrectionlevel = $this->handleErrorCorrectionLevel($errorcorrectionlevel);
2220
}
2321

24-
/**
25-
* Adds an image to the middle of the QR Code.
26-
* @param string $path Path to an image file
27-
* @param array|int $size Just the width, or [width, height]
28-
*/
29-
public function setLogo($path, $size = null)
30-
{
31-
$this->logoPath = $path;
32-
$this->logoSize = (array)$size;
33-
}
34-
3522
public function getMimeType()
3623
{
3724
return 'image/png';
3825
}
3926

4027
public function getQRCodeImage($qrtext, $size)
28+
{
29+
return $this->qrCodeInstance($qrtext, $size)->writeString();
30+
}
31+
32+
protected function qrCodeInstance($qrtext, $size)
4133
{
4234
$qrCode = new QrCode($qrtext);
4335
$qrCode->setSize($size);
@@ -47,14 +39,7 @@ public function getQRCodeImage($qrtext, $size)
4739
$qrCode->setBackgroundColor($this->bgcolor);
4840
$qrCode->setForegroundColor($this->color);
4941

50-
if ($this->logoPath) {
51-
$qrCode->setLogoPath($this->logoPath);
52-
if ($this->logoSize) {
53-
$qrCode->setLogoSize($this->logoSize[0], $this->logoSize[1]);
54-
}
55-
}
56-
57-
return $qrCode->writeString();
42+
return $qrCode;
5843
}
5944

6045
private function handleColor($color)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace RobThree\Auth\Providers\Qr;
3+
4+
use Endroid\QrCode\ErrorCorrectionLevel;
5+
use Endroid\QrCode\QrCode;
6+
7+
class EndroidQrCodeWithLogoProvider extends EndroidQrCodeProvider
8+
{
9+
protected $logoPath;
10+
protected $logoSize;
11+
12+
/**
13+
* Adds an image to the middle of the QR Code.
14+
* @param string $path Path to an image file
15+
* @param array|int $size Just the width, or [width, height]
16+
*/
17+
public function setLogo($path, $size = null)
18+
{
19+
$this->logoPath = $path;
20+
$this->logoSize = (array)$size;
21+
}
22+
23+
protected function qrCodeInstance($qrtext, $size) {
24+
$qrCode = parent::qrCodeInstance($qrtext, $size);
25+
26+
if ($this->logoPath) {
27+
$qrCode->setLogoPath($this->logoPath);
28+
if ($this->logoSize) {
29+
$qrCode->setLogoSize($this->logoSize[0], $this->logoSize[1]);
30+
}
31+
}
32+
33+
return $qrCode;
34+
}
35+
}

0 commit comments

Comments
 (0)