Browse Source

Set TwoFAccountService as static behind a Facade

Bubka 3 years ago
parent
commit
be632bb489

+ 5 - 22
app/Api/v1/Controllers/TwoFAccountController.php

@@ -14,30 +14,13 @@ use App\Api\v1\Resources\TwoFAccountCollection;
 use App\Api\v1\Resources\TwoFAccountReadResource;
 use App\Api\v1\Resources\TwoFAccountStoreResource;
 use App\Facades\Groups;
-use App\Services\TwoFAccountService;
+use App\Facades\TwoFAccounts;
 use Illuminate\Support\Arr;
 use Illuminate\Http\Request;
 use App\Http\Controllers\Controller;
 
 class TwoFAccountController extends Controller
 {
-    /**
-     * The TwoFAccount Service instance.
-     */
-    protected $twofaccountService;
-
-
-    /**
-     * Create a new controller instance.
-     *
-     * @param  \App\Services\TwoFAccountService  $twofaccountService
-     * @return void
-     */
-    public function __construct(TwoFAccountService $twofaccountService)
-    {
-        $this->twofaccountService = $twofaccountService;
-    }
-
 
     /**
      * List all resources
@@ -128,7 +111,7 @@ class TwoFAccountController extends Controller
     public function import(TwoFAccountImportRequest $request)
     { 
         $request->merge(['withSecret' => true]);
-        $twofaccounts = $this->twofaccountService->convertMigrationFromGA($request->uri);
+        $twofaccounts = TwoFAccounts::convertMigrationFromGA($request->uri);
 
         return new TwoFAccountCollection($twofaccounts);
     }
@@ -238,7 +221,7 @@ class TwoFAccountController extends Controller
             ], 400);
         }
 
-        $this->twofaccountService->withdraw($validated['ids']);
+        TwoFAccounts::withdraw($validated['ids']);
         
         return response()->json([ 'message' => 'accounts withdrawn' ], 200);
     }
@@ -252,7 +235,7 @@ class TwoFAccountController extends Controller
      */
     public function destroy(TwoFAccount $twofaccount)
     {
-        $this->twofaccountService->delete($twofaccount->id);
+        TwoFAccounts::delete($twofaccount->id);
 
         return response()->json(null, 204);
     }
@@ -275,7 +258,7 @@ class TwoFAccountController extends Controller
             ], 400);
         }
 
-        $this->twofaccountService->delete($validated['ids']);
+        TwoFAccounts::delete($validated['ids']);
 
         return response()->json(null, 204);
     }

+ 14 - 0
app/Facades/TwoFAccounts.php

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

+ 0 - 6
app/Providers/TwoFAuthServiceProvider.php

@@ -4,7 +4,6 @@ namespace App\Providers;
 
 use App\Services\LogoService;
 use App\Services\SettingService;
-use App\Services\TwoFAccountService;
 use Illuminate\Support\ServiceProvider;
 use Illuminate\Contracts\Support\DeferrableProvider;
 
@@ -24,10 +23,6 @@ class TwoFAuthServiceProvider extends ServiceProvider implements DeferrableProvi
         $this->app->singleton(LogoService::class, function () {
             return new LogoService();
         });
-
-        $this->app->singleton(TwoFAccountService::class, function () {
-            return new TwoFAccountService();
-        });
     }
 
     /**
@@ -50,7 +45,6 @@ class TwoFAuthServiceProvider extends ServiceProvider implements DeferrableProvi
     {
         return [
             LogoService::class,
-            TwoFAccountService::class,
         ];
     }
 }

+ 8 - 8
app/Services/TwoFAccountService.php

@@ -23,11 +23,11 @@ class TwoFAccountService
      * 
      * @param int|array|string $ids twofaccount ids to free
      */
-    public function withdraw($ids) : void
+    public static function withdraw($ids) : void
     {
         // $ids as string could be a comma-separated list of ids
         // so in this case we explode the string to an array
-        $ids = $this->commaSeparatedToArray($ids);
+        $ids = self::commaSeparatedToArray($ids);
 
         // whereIn() expects an array
         $ids = is_array($ids) ? $ids : func_get_args();
@@ -48,11 +48,11 @@ class TwoFAccountService
      * 
      * @return int The number of deleted
      */
-    public function delete($ids) : int
+    public static function delete($ids) : int
     {
         // $ids as string could be a comma-separated list of ids
         // so in this case we explode the string to an array
-        $ids = $this->commaSeparatedToArray($ids);
+        $ids = self::commaSeparatedToArray($ids);
         Log::info(sprintf('Deletion of TwoFAccounts #%s requested', is_array($ids) ? implode(',#', $ids) : $ids ));
         $deleted = TwoFAccount::destroy($ids);
 
@@ -67,7 +67,7 @@ class TwoFAccountService
      * 
      * @return \Illuminate\Support\Collection The converted accounts
      */
-    public function convertMigrationFromGA($migrationUri) : Collection
+    public static function convertMigrationFromGA($migrationUri) : Collection
     {
         try {
             $migrationData = base64_decode(urldecode(Str::replace('otpauth-migration://offline?data=', '', $migrationUri)));
@@ -116,7 +116,7 @@ class TwoFAccountService
              }
         }
 
-        return $this->markAsDuplicate(collect($twofaccounts));
+        return self::markAsDuplicate(collect($twofaccounts));
 
     }
 
@@ -124,7 +124,7 @@ class TwoFAccountService
     /**
      * 
      */
-    private function commaSeparatedToArray($ids)
+    private static function commaSeparatedToArray($ids)
     {
         if(is_string($ids))
         {
@@ -144,7 +144,7 @@ class TwoFAccountService
      * @param \Illuminate\Support\Collection
      * @return \Illuminate\Support\Collection
      */
-    private function markAsDuplicate($twofaccounts) : Collection
+    private static function markAsDuplicate($twofaccounts) : Collection
     {
         $storage = TwoFAccount::all();
 

+ 4 - 1
config/app.php

@@ -229,7 +229,10 @@ return [
         'URL' => Illuminate\Support\Facades\URL::class,
         'Validator' => Illuminate\Support\Facades\Validator::class,
         'View' => Illuminate\Support\Facades\View::class,
-
+        'QrCode' => App\Facades\QrCode::class,
+        'Groups' => App\Facades\Groups::class,
+        'TwoFAccounts' => App\Facades\TwoFAccounts::class,
+        
     ],
 
 ];

+ 11 - 19
tests/Feature/Services/TwoFAccountServiceTest.php

@@ -6,7 +6,7 @@ use App\Models\Group;
 use App\Models\TwoFAccount;
 use Tests\FeatureTestCase;
 use Tests\Classes\OtpTestData;
-use App\Services\TwoFAccountService;
+use App\Facades\TwoFAccounts;
 
 
 /**
@@ -14,12 +14,6 @@ use App\Services\TwoFAccountService;
  */
 class TwoFAccountServiceTest extends FeatureTestCase
 {
-    /**
-     * App\Services\SettingService $settingService
-     */
-    protected $twofaccountService;
-
-
     /**
      * App\Models\TwoFAccount $customTotpTwofaccount
      */
@@ -45,8 +39,6 @@ class TwoFAccountServiceTest extends FeatureTestCase
     {
         parent::setUp();
 
-        $this->twofaccountService = $this->app->make(TwoFAccountService::class);
-
         $this->customTotpTwofaccount = new TwoFAccount;
         $this->customTotpTwofaccount->legacy_uri = OtpTestData::TOTP_FULL_CUSTOM_URI;
         $this->customTotpTwofaccount->service = OtpTestData::SERVICE;
@@ -88,7 +80,7 @@ class TwoFAccountServiceTest extends FeatureTestCase
         $twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]);
         $this->group->twofaccounts()->saveMany($twofaccounts);
         
-        $this->twofaccountService->withdraw($this->customHotpTwofaccount->id.','.$this->customTotpTwofaccount->id);
+        TwoFAccounts::withdraw($this->customHotpTwofaccount->id.','.$this->customTotpTwofaccount->id);
 
         $this->assertDatabaseHas('twofaccounts', [
             'id'      => $this->customTotpTwofaccount->id,
@@ -110,7 +102,7 @@ class TwoFAccountServiceTest extends FeatureTestCase
         $twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]);
         $this->group->twofaccounts()->saveMany($twofaccounts);
         
-        $this->twofaccountService->withdraw([$this->customHotpTwofaccount->id, $this->customTotpTwofaccount->id]);
+        TwoFAccounts::withdraw([$this->customHotpTwofaccount->id, $this->customTotpTwofaccount->id]);
 
         $this->assertDatabaseHas('twofaccounts', [
             'id'      => $this->customTotpTwofaccount->id,
@@ -132,7 +124,7 @@ class TwoFAccountServiceTest extends FeatureTestCase
         $twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]);
         $this->group->twofaccounts()->saveMany($twofaccounts);
         
-        $this->twofaccountService->withdraw($this->customTotpTwofaccount->id);
+        TwoFAccounts::withdraw($this->customTotpTwofaccount->id);
 
         $this->assertDatabaseHas('twofaccounts', [
             'id'      => $this->customTotpTwofaccount->id,
@@ -146,7 +138,7 @@ class TwoFAccountServiceTest extends FeatureTestCase
      */
     public function test_withdraw_missing_ids_returns_void()
     {
-        $this->assertNull($this->twofaccountService->withdraw(null));
+        $this->assertNull(TwoFAccounts::withdraw(null));
     }
 
     
@@ -155,7 +147,7 @@ class TwoFAccountServiceTest extends FeatureTestCase
      */
     public function test_delete_comma_separated_ids()
     {        
-        $this->twofaccountService->delete($this->customHotpTwofaccount->id.','.$this->customTotpTwofaccount->id);
+        TwoFAccounts::delete($this->customHotpTwofaccount->id.','.$this->customTotpTwofaccount->id);
 
         $this->assertDatabaseMissing('twofaccounts', [
             'id'      => $this->customTotpTwofaccount->id,
@@ -171,7 +163,7 @@ class TwoFAccountServiceTest extends FeatureTestCase
      */
     public function test_delete_array_of_ids()
     {        
-        $this->twofaccountService->delete([$this->customTotpTwofaccount->id, $this->customHotpTwofaccount->id]);
+        TwoFAccounts::delete([$this->customTotpTwofaccount->id, $this->customHotpTwofaccount->id]);
 
         $this->assertDatabaseMissing('twofaccounts', [
             'id'      => $this->customTotpTwofaccount->id,
@@ -187,7 +179,7 @@ class TwoFAccountServiceTest extends FeatureTestCase
      */
     public function test_delete_single_id()
     {        
-        $this->twofaccountService->delete($this->customTotpTwofaccount->id);
+        TwoFAccounts::delete($this->customTotpTwofaccount->id);
 
         $this->assertDatabaseMissing('twofaccounts', [
             'id'      => $this->customTotpTwofaccount->id,
@@ -200,7 +192,7 @@ class TwoFAccountServiceTest extends FeatureTestCase
      */
     public function test_convert_migration_from_gauth_returns_correct_accounts()
     {        
-        $twofaccounts = $this->twofaccountService->convertMigrationFromGA(OtpTestData::GOOGLE_AUTH_MIGRATION_URI);
+        $twofaccounts = TwoFAccounts::convertMigrationFromGA(OtpTestData::GOOGLE_AUTH_MIGRATION_URI);
 
         $this->assertCount(2, $twofaccounts);
 
@@ -248,7 +240,7 @@ class TwoFAccountServiceTest extends FeatureTestCase
         $twofaccount = new TwoFAccount;
         $twofaccount->fillWithOtpParameters($parameters)->save();
 
-        $twofaccounts = $this->twofaccountService->convertMigrationFromGA(OtpTestData::GOOGLE_AUTH_MIGRATION_URI);
+        $twofaccounts = TwoFAccounts::convertMigrationFromGA(OtpTestData::GOOGLE_AUTH_MIGRATION_URI);
 
         $this->assertEquals(-1, $twofaccounts->first()->id);
         $this->assertEquals(-1, $twofaccounts->last()->id);
@@ -261,7 +253,7 @@ class TwoFAccountServiceTest extends FeatureTestCase
     public function test_convert_invalid_migration_from_gauth_returns_InvalidGoogleAuthMigration_excpetion()
     {
         $this->expectException(\App\Exceptions\InvalidGoogleAuthMigration::class);
-        $twofaccounts = $this->twofaccountService->convertMigrationFromGA(OtpTestData::GOOGLE_AUTH_MIGRATION_URI_WITH_INVALID_DATA);
+        $twofaccounts = TwoFAccounts::convertMigrationFromGA(OtpTestData::GOOGLE_AUTH_MIGRATION_URI_WITH_INVALID_DATA);
     }
 
 }