Skip to content

Commit 63f6259

Browse files
committed
Added support for EndroidQR v5
1 parent 137df4d commit 63f6259

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"source": "https://github.com/RobThree/TwoFactorAuth"
2828
},
2929
"require": {
30-
"php": ">=8.1.0"
30+
"php": ">=8.1.0",
31+
"endroid/qr-code": "^5.0"
3132
},
3233
"require-dev": {
3334
"phpunit/phpunit": "^9",

lib/Providers/Qr/EndroidQrCodeProvider.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ class EndroidQrCodeProvider implements IQRCodeProvider
2626

2727
protected $endroid4 = false;
2828

29+
protected $endroid5 = false;
30+
2931
public function __construct($bgcolor = 'ffffff', $color = '000000', $margin = 0, $errorcorrectionlevel = 'H')
3032
{
3133
$this->endroid4 = method_exists(QrCode::class, 'create');
34+
$this->endroid5 = enum_exists(ErrorCorrectionLevel::class);
3235

3336
$this->bgcolor = $this->handleColor($bgcolor);
3437
$this->color = $this->handleColor($color);
@@ -76,11 +79,30 @@ private function handleColor(string $color): Color|array
7679

7780
private function handleErrorCorrectionLevel(string $level): ErrorCorrectionLevelInterface|ErrorCorrectionLevel
7881
{
82+
if ($this->endroid4) {
83+
return match ($level) {
84+
'L' => new ErrorCorrectionLevelLow(),
85+
'M' => new ErrorCorrectionLevelMedium(),
86+
'Q' => new ErrorCorrectionLevelQuartile(),
87+
default => new ErrorCorrectionLevelHigh(),
88+
};
89+
}
90+
91+
if ($this->endroid5) {
92+
return match ($level) {
93+
'L' => ErrorCorrectionLevel::Low,
94+
'M' => ErrorCorrectionLevel::Medium,
95+
'Q' => ErrorCorrectionLevel::Quartile,
96+
default => ErrorCorrectionLevel::High,
97+
};
98+
}
99+
100+
// Assuming this is for version EndroidQR < 4
79101
return match ($level) {
80-
'L' => $this->endroid4 ? new ErrorCorrectionLevelLow() : ErrorCorrectionLevel::LOW(),
81-
'M' => $this->endroid4 ? new ErrorCorrectionLevelMedium() : ErrorCorrectionLevel::MEDIUM(),
82-
'Q' => $this->endroid4 ? new ErrorCorrectionLevelQuartile() : ErrorCorrectionLevel::QUARTILE(),
83-
default => $this->endroid4 ? new ErrorCorrectionLevelHigh() : ErrorCorrectionLevel::HIGH(),
102+
'L' => ErrorCorrectionLevel::LOW(),
103+
'M' => ErrorCorrectionLevel::MEDIUM(),
104+
'Q' => ErrorCorrectionLevel::QUARTILE(),
105+
default => ErrorCorrectionLevel::HIGH(),
84106
};
85107
}
86108
}

0 commit comments

Comments
 (0)