Skip to content

Commit dc681e1

Browse files
committed
Merge remote-tracking branch 'mpeveler/bugfix-endroid-4'
2 parents 5d36d4f + 5f2611c commit dc681e1

File tree

4 files changed

+115
-11
lines changed

4 files changed

+115
-11
lines changed

.github/workflows/test-endroid.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Test Endroid QR Code Provider
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
11+
strategy:
12+
matrix:
13+
php-version: ['8.0', '8.1']
14+
endroid-version: ["^4"]
15+
include:
16+
- php-version: 5.6
17+
# earliest supported version
18+
endroid-version: 2.2.1
19+
- php-version: 7.0
20+
endroid-version: 2.5.1
21+
- php-version: 7.1
22+
# this version is 7.1+
23+
endroid-version: 3.0.0
24+
- php-version: 7.2
25+
# all later versions are 7.3+
26+
endroid-version: 3.5.9
27+
- php-version: 7.3
28+
endroid-version: 3.9.7
29+
- php-version: 7.4
30+
endroid-version: 4.0.0
31+
32+
steps:
33+
- uses: actions/checkout@v2
34+
35+
- uses: shivammathur/setup-php@v2
36+
with:
37+
php-version: ${{ matrix.php-version }}
38+
tools: composer
39+
coverage: xdebug
40+
ini-values: error_reporting=E_ALL
41+
42+
- uses: ramsey/composer-install@v1
43+
44+
- run: composer require endroid/qrcode:${{ matrix.endroid-version }}
45+
46+
- run: composer test testsDependency/EndroidQRCodeTest.php

lib/Providers/Qr/EndroidQrCodeProvider.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
<?php
22
namespace RobThree\Auth\Providers\Qr;
33

4+
use Endroid\QrCode\Color\Color;
45
use Endroid\QrCode\ErrorCorrectionLevel;
6+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
7+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
8+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelMedium;
9+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelQuartile;
510
use Endroid\QrCode\QrCode;
11+
use Endroid\QrCode\Writer\PngWriter;
612

713
class EndroidQrCodeProvider implements IQRCodeProvider
814
{
@@ -11,8 +17,12 @@ class EndroidQrCodeProvider implements IQRCodeProvider
1117
public $margin;
1218
public $errorcorrectionlevel;
1319

20+
protected $endroid4 = false;
21+
1422
public function __construct($bgcolor = 'ffffff', $color = '000000', $margin = 0, $errorcorrectionlevel = 'H')
1523
{
24+
$this->endroid4 = method_exists(QrCode::class, 'create');
25+
1626
$this->bgcolor = $this->handleColor($bgcolor);
1727
$this->color = $this->handleColor($color);
1828
$this->margin = $margin;
@@ -26,7 +36,12 @@ public function getMimeType()
2636

2737
public function getQRCodeImage($qrtext, $size)
2838
{
29-
return $this->qrCodeInstance($qrtext, $size)->writeString();
39+
if (!$this->endroid4) {
40+
return $this->qrCodeInstance($qrtext, $size)->writeString();
41+
}
42+
43+
$writer = new PngWriter();
44+
return $writer->write($this->qrCodeInstance($qrtext, $size))->getString();
3045
}
3146

3247
protected function qrCodeInstance($qrtext, $size)
@@ -49,22 +64,21 @@ private function handleColor($color)
4964
$g = hexdec($split[1]);
5065
$b = hexdec($split[2]);
5166

52-
return ['r' => $r, 'g' => $g, 'b' => $b, 'a' => 0];
67+
return $this->endroid4 ? new Color($r, $g, $b, 0) : ['r' => $r, 'g' => $g, 'b' => $b, 'a' => 0];
5368
}
5469

5570
private function handleErrorCorrectionLevel($level)
5671
{
5772
switch ($level) {
5873
case 'L':
59-
return ErrorCorrectionLevel::LOW();
74+
return $this->endroid4 ? new ErrorCorrectionLevelLow() : ErrorCorrectionLevel::LOW();
6075
case 'M':
61-
return ErrorCorrectionLevel::MEDIUM();
76+
return $this->endroid4 ? new ErrorCorrectionLevelMedium() : ErrorCorrectionLevel::MEDIUM();
6277
case 'Q':
63-
return ErrorCorrectionLevel::QUARTILE();
78+
return $this->endroid4 ? new ErrorCorrectionLevelQuartile() : ErrorCorrectionLevel::QUARTILE();
6479
case 'H':
65-
return ErrorCorrectionLevel::HIGH();
6680
default:
67-
return ErrorCorrectionLevel::HIGH();
81+
return $this->endroid4 ? new ErrorCorrectionLevelHigh() : ErrorCorrectionLevel::HIGH();
6882
}
6983
}
7084
}

lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
namespace RobThree\Auth\Providers\Qr;
33

4-
use Endroid\QrCode\ErrorCorrectionLevel;
5-
use Endroid\QrCode\QrCode;
4+
use Endroid\QrCode\Logo\Logo;
5+
use Endroid\QrCode\Writer\PngWriter;
66

77
class EndroidQrCodeWithLogoProvider extends EndroidQrCodeProvider
88
{
@@ -20,13 +20,33 @@ public function setLogo($path, $size = null)
2020
$this->logoSize = (array)$size;
2121
}
2222

23+
public function getQRCodeImage($qrtext, $size)
24+
{
25+
if (!$this->endroid4) {
26+
return $this->qrCodeInstance($qrtext, $size)->writeString();
27+
}
28+
29+
$logo = null;
30+
if ($this->logoPath) {
31+
$logo = Logo::create($this->logoPath);
32+
if ($this->logoSize) {
33+
$logo->setResizeToWidth($this->logoSize[0]);
34+
if (isset($this->logoSize[1])) {
35+
$logo->setResizeToHeight($this->logoSize[1]);
36+
}
37+
}
38+
}
39+
$writer = new PngWriter();
40+
return $writer->write($this->qrCodeInstance($qrtext, $size), $logo)->getString();
41+
}
42+
2343
protected function qrCodeInstance($qrtext, $size) {
2444
$qrCode = parent::qrCodeInstance($qrtext, $size);
2545

26-
if ($this->logoPath) {
46+
if (!$this->endroid4 && $this->logoPath) {
2747
$qrCode->setLogoPath($this->logoPath);
2848
if ($this->logoSize) {
29-
$qrCode->setLogoSize($this->logoSize[0], $this->logoSize[1]);
49+
$qrCode->setLogoSize($this->logoSize[0], isset($this->logoSize[1]) ? $this->logoSize[1] : null);
3050
}
3151
}
3252

testsDependency/EndroidQRCodeTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace TestsDependency;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use RobThree\Auth\TwoFactorAuth;
7+
use RobThree\Auth\Providers\Qr\EndroidQrCodeProvider;
8+
use RobThree\Auth\Providers\Qr\HandlesDataUri;
9+
10+
class EndroidQRCodeTest extends TestCase
11+
{
12+
use HandlesDataUri;
13+
14+
public function testDependency()
15+
{
16+
$qr = new EndroidQrCodeProvider();
17+
$tfa = new TwoFactorAuth('Test&Issuer', 6, 30, 'sha1', $qr);
18+
$data = $this->DecodeDataUri($tfa->getQRCodeImageAsDataUri('Test&Label', 'VMR466AB62ZBOKHE'));
19+
$this->assertEquals('image/png', $data['mimetype']);
20+
$this->assertEquals('base64', $data['encoding']);
21+
$this->assertNotEmpty($data['data']);
22+
23+
}
24+
}

0 commit comments

Comments
 (0)