Set QrCodeService as static behind a Facade

This commit is contained in:
Bubka 2022-07-30 10:46:02 +02:00
parent e2bfbe5868
commit 2fcaf46c9d
5 changed files with 23 additions and 41 deletions

View File

@ -3,30 +3,13 @@
namespace App\Api\v1\Controllers; namespace App\Api\v1\Controllers;
use App\Models\TwoFAccount; use App\Models\TwoFAccount;
use App\Services\QrCodeService; use App\Facades\QrCode;
use App\Api\v1\Requests\QrCodeDecodeRequest; use App\Api\v1\Requests\QrCodeDecodeRequest;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
class QrCodeController extends Controller class QrCodeController extends Controller
{ {
/**
* The QR code Service instance.
*/
protected $qrcodeService;
/**
* Create a new controller instance.
*
* @param \App\Services\QrCodeService $qrcodeService
* @return void
*/
public function __construct(QrCodeService $qrcodeService)
{
$this->qrcodeService = $qrcodeService;
}
/** /**
* Show a QR code image * Show a QR code image
@ -38,7 +21,7 @@ public function show(TwoFAccount $twofaccount)
{ {
$uri = $twofaccount->getURI(); $uri = $twofaccount->getURI();
return response()->json(['qrcode' => $this->qrcodeService->encode($uri)], 200); return response()->json(['qrcode' => QrCode::encode($uri)], 200);
} }
@ -52,7 +35,7 @@ public function decode(QrCodeDecodeRequest $request)
{ {
$file = $request->file('qrcode'); $file = $request->file('qrcode');
return response()->json(['data' => $this->qrcodeService->decode($file)], 200); return response()->json(['data' => QrCode::decode($file)], 200);
} }
} }

14
app/Facades/QrCode.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace App\Facades;
use App\Services\QrCodeService;
use Illuminate\Support\Facades\Facade;
class QrCode extends Facade
{
protected static function getFacadeAccessor()
{
return QrCodeService::class;
}
}

View File

@ -3,7 +3,6 @@
namespace App\Providers; namespace App\Providers;
use App\Services\LogoService; use App\Services\LogoService;
use App\Services\QrCodeService;
use App\Services\SettingService; use App\Services\SettingService;
use App\Services\GroupService; use App\Services\GroupService;
use App\Services\TwoFAccountService; use App\Services\TwoFAccountService;
@ -31,10 +30,6 @@ public function register()
return new LogoService(); return new LogoService();
}); });
$this->app->singleton(QrCodeService::class, function () {
return new QrCodeService();
});
$this->app->singleton(TwoFAccountService::class, function () { $this->app->singleton(TwoFAccountService::class, function () {
return new TwoFAccountService(); return new TwoFAccountService();
}); });
@ -61,7 +56,6 @@ public function provides()
return [ return [
GroupService::class, GroupService::class,
LogoService::class, LogoService::class,
QrCodeService::class,
TwoFAccountService::class, TwoFAccountService::class,
]; ];
} }

View File

@ -15,7 +15,7 @@ class QrCodeService
* *
* @return mixed * @return mixed
*/ */
public function encode(string $data) public static function encode(string $data)
{ {
$options = new QROptions([ $options = new QROptions([
'quietzoneSize' => 2, 'quietzoneSize' => 2,
@ -35,7 +35,7 @@ public function encode(string $data)
* *
* @param \Illuminate\Http\UploadedFile $file * @param \Illuminate\Http\UploadedFile $file
*/ */
public function decode(\Illuminate\Http\UploadedFile $file) public static function decode(\Illuminate\Http\UploadedFile $file)
{ {
$qrcode = new QrReader($file->get(), QrReader::SOURCE_TYPE_BLOB); $qrcode = new QrReader($file->get(), QrReader::SOURCE_TYPE_BLOB);
$data = urldecode($qrcode->text()); $data = urldecode($qrcode->text());

View File

@ -4,19 +4,13 @@
use Tests\FeatureTestCase; use Tests\FeatureTestCase;
use Tests\Classes\LocalFile; use Tests\Classes\LocalFile;
use App\Services\QrCodeService; use App\Facades\QrCode;
/** /**
* @covers \App\Services\QrCodeService * @covers \App\Services\QrCodeService
*/ */
class QrCodeServiceTest extends FeatureTestCase class QrCodeServiceTest extends FeatureTestCase
{ {
/**
* App\Services\QrCodeService $qrcodeService
*/
protected $qrcodeService;
private const STRING_TO_ENCODE = 'stringToEncode'; private const STRING_TO_ENCODE = 'stringToEncode';
private const STRING_ENCODED = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAIAAAAiOjnJAAAABnRSTlMA/wD/AP83WBt9AAAACXBIWXMAAA7EAAAOxAGVKw4bAAADOklEQVR4nO3dMW7kMBAAwdPh/v/ldeBLFRCYBkm7KveaEBoMBhT1fD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AiLhLBICIuEsEgIi8TYHOu080mrpt7LO+3+qqn75VfZsUgIi4SwSAiLhLBICIuEsEg8t5yX4i52LBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEl+BmgChoSs9XAAAAABJRU5ErkJggg==';
private const DECODED_IMAGE = 'otpauth://totp/test@test.com?secret=A4GRFHVIRBGY7UIW'; private const DECODED_IMAGE = 'otpauth://totp/test@test.com?secret=A4GRFHVIRBGY7UIW';
@ -28,8 +22,6 @@ class QrCodeServiceTest extends FeatureTestCase
public function setUp() : void public function setUp() : void
{ {
parent::setUp(); parent::setUp();
$this->qrcodeService = $this->app->make(QrCodeService::class);
} }
@ -38,8 +30,7 @@ public function setUp() : void
*/ */
public function test_encode_returns_correct_value() public function test_encode_returns_correct_value()
{ {
// $rendered = $this->qrcodeService->encode(self::STRING_TO_ENCODE); $this->assertEquals(self::STRING_ENCODED, QrCode::encode(self::STRING_TO_ENCODE));
$this->assertEquals(self::STRING_ENCODED, $this->qrcodeService->encode(self::STRING_TO_ENCODE));
} }
@ -50,7 +41,7 @@ public function test_decode_valid_image_returns_correct_value()
{ {
$file = LocalFile::fake()->validQrcode(); $file = LocalFile::fake()->validQrcode();
$this->assertEquals(self::DECODED_IMAGE, $this->qrcodeService->decode($file)); $this->assertEquals(self::DECODED_IMAGE, QrCode::decode($file));
} }
@ -61,7 +52,7 @@ public function test_decode_invalid_image_returns_correct_value()
{ {
$this->expectException(\App\Exceptions\InvalidQrCodeException::class); $this->expectException(\App\Exceptions\InvalidQrCodeException::class);
$this->qrcodeService->decode(LocalFile::fake()->invalidQrcode()); QrCode::decode(LocalFile::fake()->invalidQrcode());
} }
} }