Browse Source

Set GroupService as static behind a Facade

Bubka 3 years ago
parent
commit
601d50e8a4

+ 7 - 23
app/Api/v1/Controllers/GroupController.php

@@ -3,7 +3,7 @@
 namespace App\Api\v1\Controllers;
 
 use App\Models\Group;
-use App\Services\GroupService;
+use App\Facades\Groups;
 use App\Api\v1\Requests\GroupStoreRequest;
 use App\Api\v1\Requests\GroupAssignRequest;
 use App\Api\v1\Resources\GroupResource;
@@ -13,22 +13,6 @@ use Illuminate\Support\Facades\App;
 
 class GroupController extends Controller
 {
-    /**
-     * The TwoFAccount Service instance.
-     */
-    protected $groupService;
-
-
-    /**
-     * Create a new controller instance.
-     *
-     * @return void
-     */
-    public function __construct()
-    {
-        $this->groupService = App::make(GroupService::class);
-    }
-
 
     /**
      * Display a listing of the resource.
@@ -37,7 +21,7 @@ class GroupController extends Controller
      */
     public function index()
     {
-        $groups = $this->groupService->getAll();
+        $groups = Groups::getAll();
 
         return GroupResource::collection($groups);
     }
@@ -53,7 +37,7 @@ class GroupController extends Controller
     {
         $validated = $request->validated();
 
-        $group = $this->groupService->create($validated);
+        $group = Groups::create($validated);
 
         return (new GroupResource($group))
             ->response()
@@ -84,7 +68,7 @@ class GroupController extends Controller
     {
         $validated = $request->validated();
 
-        $this->groupService->update($group, $validated);
+        Groups::update($group, $validated);
 
         return new GroupResource($group);
 
@@ -102,7 +86,7 @@ class GroupController extends Controller
     {
         $validated = $request->validated();
 
-        $this->groupService->assign($validated['ids'], $group);
+        Groups::assign($validated['ids'], $group);
             
         return new GroupResource($group);
 
@@ -117,7 +101,7 @@ class GroupController extends Controller
      */
     public function accounts(Group $group)
     {
-        $twofaccounts = $this->groupService->getAccounts($group);
+        $twofaccounts = Groups::getAccounts($group);
             
         return new TwoFAccountCollection($twofaccounts);
 
@@ -132,7 +116,7 @@ class GroupController extends Controller
      */
     public function destroy(Group $group)
     {
-        $this->groupService->delete($group->id);
+        Groups::delete($group->id);
 
         return response()->json(null, 204);
     }

+ 3 - 10
app/Api/v1/Controllers/TwoFAccountController.php

@@ -13,7 +13,7 @@ use App\Api\v1\Requests\TwoFAccountDynamicRequest;
 use App\Api\v1\Resources\TwoFAccountCollection;
 use App\Api\v1\Resources\TwoFAccountReadResource;
 use App\Api\v1\Resources\TwoFAccountStoreResource;
-use App\Services\GroupService;
+use App\Facades\Groups;
 use App\Services\TwoFAccountService;
 use Illuminate\Support\Arr;
 use Illuminate\Http\Request;
@@ -26,23 +26,16 @@ class TwoFAccountController extends Controller
      */
     protected $twofaccountService;
 
-    /**
-     * The Group Service instance.
-     */
-    protected $groupService;
-
 
     /**
      * Create a new controller instance.
      *
      * @param  \App\Services\TwoFAccountService  $twofaccountService
-     * @param  \App\Services\GroupService  $groupService
      * @return void
      */
-    public function __construct(TwoFAccountService $twofaccountService, GroupService $groupService)
+    public function __construct(TwoFAccountService $twofaccountService)
     {
         $this->twofaccountService = $twofaccountService;
-        $this->groupService = $groupService;
     }
 
 
@@ -96,7 +89,7 @@ class TwoFAccountController extends Controller
         $twofaccount->save();
 
         // Possible group association
-        $this->groupService->assign($twofaccount->id);
+        Groups::assign($twofaccount->id);
 
         return (new TwoFAccountReadResource($twofaccount->refresh()))
                 ->response()

+ 14 - 0
app/Facades/Groups.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Facades;
+
+use App\Services\GroupService;
+use Illuminate\Support\Facades\Facade;
+
+class Groups extends Facade
+{
+    protected static function getFacadeAccessor()
+    {
+        return GroupService::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\GroupService;
 use App\Services\TwoFAccountService;
 use Illuminate\Support\ServiceProvider;
 use Illuminate\Contracts\Support\DeferrableProvider;
@@ -22,10 +21,6 @@ class TwoFAuthServiceProvider extends ServiceProvider implements DeferrableProvi
             return new SettingService();
         });
 
-        $this->app->singleton(GroupService::class, function ($app) {
-            return new GroupService($app->make(SettingService::class));
-        });
-
         $this->app->singleton(LogoService::class, function () {
             return new LogoService();
         });
@@ -54,7 +49,6 @@ class TwoFAuthServiceProvider extends ServiceProvider implements DeferrableProvi
     public function provides()
     {
         return [
-            GroupService::class,
             LogoService::class,
             TwoFAccountService::class,
         ];

+ 17 - 30
app/Services/GroupService.php

@@ -7,32 +7,16 @@ use App\Models\TwoFAccount;
 use App\Services\SettingService;
 use Illuminate\Database\Eloquent\Collection;
 use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\App;
 
 class GroupService
 {
-
-    /**
-     * The Settings Service instance.
-     */
-    protected SettingService $settingService;
-
-
-    /**
-     * Create a new controller instance.
-     * 
-     */
-    public function __construct(SettingService $settingService)
-    {
-        $this->settingService = $settingService;
-    }
-
-
     /**
      * Returns all existing groups
      * 
      * @return Collection
      */
-    public function getAll() : Collection
+    public static function getAll() : Collection
     {
         // We return the complete collection of groups
         // stored in db plus a pseudo group corresponding to 'All'
@@ -61,7 +45,7 @@ class GroupService
      * @param array $data
      * @return \App\Models\Group The created group
      */
-    public function create(array $data) : Group
+    public static function create(array $data) : Group
     {
         $group = Group::create([
             'name' => $data['name'],
@@ -82,7 +66,7 @@ class GroupService
      * @param array $data The parameters
      * @return \App\Models\Group The updated group
      */
-    public function update(Group $group, array $data) : Group
+    public static function update(Group $group, array $data) : Group
     {
         $group->update([
             'name' => $data['name'],
@@ -100,25 +84,27 @@ class GroupService
      * @param int|array $ids group ids to delete
      * @return int The number of deleted
      */
-    public function delete($ids) : int
+    public static function delete($ids) : int
     {
+        $settingService = App::make(SettingService::class);
+
         $ids = is_array($ids) ? $ids : func_get_args();
 
         // A group is possibly set as the default group in Settings.
         // In this case we reset the setting to "No group" (groupId = 0)
-        $defaultGroupId = $this->settingService->get('defaultGroup');
+        $defaultGroupId = $settingService->get('defaultGroup');
 
         if (in_array($defaultGroupId, $ids)) {
-            $this->settingService->set('defaultGroup', 0);
+            $settingService->set('defaultGroup', 0);
         }
 
         // A group is also possibly set as the active group if the user
         // configured 2FAuth to memorize the active group.
         // In this case we reset the setting to the pseudo "All" group (groupId = 0)
-        $activeGroupId = $this->settingService->get('activeGroup');
+        $activeGroupId = $settingService->get('activeGroup');
 
         if (in_array($activeGroupId, $ids)) {
-            $this->settingService->set('activeGroup', 0);
+            $settingService->set('activeGroup', 0);
         }
 
         $deleted = Group::destroy($ids);
@@ -136,10 +122,10 @@ class GroupService
      * @param \App\Models\Group $group The target group
      * @return void
      */
-    public function assign($ids, Group $group = null) : void
+    public static function assign($ids, Group $group = null) : void
     {
         if (!$group) {
-            $group = $this->defaultGroup();
+            $group = self::defaultGroup();
         }
 
         if ($group) {
@@ -165,7 +151,7 @@ class GroupService
      * @param \App\Models\Group $group The group
      * @return Collection The assigned accounts
      */
-    public function getAccounts(Group $group) : Collection
+    public static function getAccounts(Group $group) : Collection
     {
         $twofaccounts = $group->twofaccounts()->where('group_id', $group->id)->get();
 
@@ -178,9 +164,10 @@ class GroupService
      * 
      * @return \App\Models\Group|null The group or null if it does not exist
      */
-    private function defaultGroup()
+    private static function defaultGroup()
     {
-        $id = $this->settingService->get('defaultGroup') === -1 ? (int) $this->settingService->get('activeGroup') : (int) $this->settingService->get('defaultGroup');
+        $settingService = App::make(SettingService::class);
+        $id = $settingService->get('defaultGroup') === -1 ? (int) $settingService->get('activeGroup') : (int) $settingService->get('defaultGroup');
 
         return Group::find($id);
     }

+ 17 - 24
tests/Feature/Services/GroupServiceTest.php

@@ -5,7 +5,7 @@ namespace Tests\Feature\Services;
 use App\Models\Group;
 use App\Models\TwoFAccount;
 use Tests\FeatureTestCase;
-use App\Services\GroupService;
+use App\Facades\Groups;
 use App\Services\SettingService;
 
 
@@ -14,12 +14,6 @@ use App\Services\SettingService;
  */
 class GroupServiceTest extends FeatureTestCase
 {
-    /**
-     * App\Services\GroupService $groupService
-     */
-    protected $groupService;
-
-
     /**
      * App\Services\SettingService $settingService
      */
@@ -58,7 +52,6 @@ class GroupServiceTest extends FeatureTestCase
     {
         parent::setUp();
 
-        $this->groupService = $this->app->make(GroupService::class);
         $this->settingService = $this->app->make(SettingService::class);
 
         $this->groupOne = new Group;
@@ -102,7 +95,7 @@ class GroupServiceTest extends FeatureTestCase
      */
     public function test_getAll_returns_a_collection()
     {
-        $this->assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, $this->groupService->getAll());
+        $this->assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, Groups::getAll());
     }
 
 
@@ -111,7 +104,7 @@ class GroupServiceTest extends FeatureTestCase
      */
     public function test_getAll_adds_pseudo_group_on_top_of_user_groups()
     {
-        $groups = $this->groupService->getAll();
+        $groups = Groups::getAll();
         
         $this->assertEquals(0, $groups->first()->id);
         $this->assertEquals(__('commons.all'), $groups->first()->name);
@@ -123,7 +116,7 @@ class GroupServiceTest extends FeatureTestCase
      */
     public function test_getAll_returns_pseudo_group_with_all_twofaccounts_count()
     {
-        $groups = $this->groupService->getAll();
+        $groups = Groups::getAll();
         
         $this->assertEquals(self::TWOFACCOUNT_COUNT, $groups->first()->twofaccounts_count);
     }
@@ -134,7 +127,7 @@ class GroupServiceTest extends FeatureTestCase
      */
     public function test_create_persists_and_returns_created_group()
     {
-        $newGroup = $this->groupService->create(['name' => self::NEW_GROUP_NAME]);
+        $newGroup = Groups::create(['name' => self::NEW_GROUP_NAME]);
         
         $this->assertDatabaseHas('groups', ['name' => self::NEW_GROUP_NAME]);
         $this->assertInstanceOf(\App\Models\Group::class, $newGroup);
@@ -147,7 +140,7 @@ class GroupServiceTest extends FeatureTestCase
      */
     public function test_update_persists_and_returns_updated_group()
     {
-        $this->groupOne = $this->groupService->update($this->groupOne, ['name' => self::NEW_GROUP_NAME]);
+        $this->groupOne = Groups::update($this->groupOne, ['name' => self::NEW_GROUP_NAME]);
         
         $this->assertDatabaseHas('groups', ['name' => self::NEW_GROUP_NAME]);
         $this->assertInstanceOf(\App\Models\Group::class, $this->groupOne);
@@ -160,7 +153,7 @@ class GroupServiceTest extends FeatureTestCase
      */
     public function test_delete_a_groupId_clear_db_and_returns_deleted_count()
     {
-        $deleted = $this->groupService->delete($this->groupOne->id);
+        $deleted = Groups::delete($this->groupOne->id);
         
         $this->assertDatabaseMissing('groups', ['id' => $this->groupOne->id]);
         $this->assertEquals(1, $deleted);
@@ -172,7 +165,7 @@ class GroupServiceTest extends FeatureTestCase
      */
     public function test_delete_an_array_of_ids_clear_db_and_returns_deleted_count()
     {
-        $deleted = $this->groupService->delete([$this->groupOne->id, $this->groupTwo->id]);
+        $deleted = Groups::delete([$this->groupOne->id, $this->groupTwo->id]);
         
         $this->assertDatabaseMissing('groups', ['id' => $this->groupOne->id]);
         $this->assertDatabaseMissing('groups', ['id' => $this->groupTwo->id]);
@@ -187,7 +180,7 @@ class GroupServiceTest extends FeatureTestCase
     {
         $this->settingService->set('defaultGroup', $this->groupOne->id);
 
-        $deleted = $this->groupService->delete($this->groupOne->id);
+        $deleted = Groups::delete($this->groupOne->id);
         
         $this->assertDatabaseHas('options', [
             'key' => 'defaultGroup',
@@ -204,7 +197,7 @@ class GroupServiceTest extends FeatureTestCase
         $this->settingService->set('rememberActiveGroup', true);
         $this->settingService->set('activeGroup', $this->groupOne->id);
         
-        $deleted = $this->groupService->delete($this->groupOne->id);
+        $deleted = Groups::delete($this->groupOne->id);
         
         $this->assertDatabaseHas('options', [
             'key' => 'activeGroup',
@@ -219,7 +212,7 @@ class GroupServiceTest extends FeatureTestCase
     public function test_assign_a_twofaccountid_to_a_specified_group_persists_the_relation()
     {
         
-        $this->groupService->assign($this->twofaccountOne->id, $this->groupOne);
+        Groups::assign($this->twofaccountOne->id, $this->groupOne);
         
         $this->assertDatabaseHas('twofaccounts', [
             'id' => $this->twofaccountOne->id,
@@ -233,7 +226,7 @@ class GroupServiceTest extends FeatureTestCase
      */
     public function test_assign_multiple_twofaccountid_to_a_specified_group_persists_the_relation()
     {
-        $this->groupService->assign([$this->twofaccountOne->id, $this->twofaccountTwo->id], $this->groupOne);
+        Groups::assign([$this->twofaccountOne->id, $this->twofaccountTwo->id], $this->groupOne);
         
         $this->assertDatabaseHas('twofaccounts', [
             'id' => $this->twofaccountOne->id,
@@ -253,7 +246,7 @@ class GroupServiceTest extends FeatureTestCase
     {
         $this->settingService->set('defaultGroup', $this->groupTwo->id);
         
-        $this->groupService->assign($this->twofaccountOne->id);
+        Groups::assign($this->twofaccountOne->id);
         
         $this->assertDatabaseHas('twofaccounts', [
             'id' => $this->twofaccountOne->id,
@@ -270,7 +263,7 @@ class GroupServiceTest extends FeatureTestCase
         $this->settingService->set('defaultGroup', -1);
         $this->settingService->set('activeGroup', $this->groupTwo->id);
         
-        $this->groupService->assign($this->twofaccountOne->id);
+        Groups::assign($this->twofaccountOne->id);
         
         $this->assertDatabaseHas('twofaccounts', [
             'id' => $this->twofaccountOne->id,
@@ -287,7 +280,7 @@ class GroupServiceTest extends FeatureTestCase
         $this->settingService->set('defaultGroup', -1);
         $this->settingService->set('activeGroup', 100000);
         
-        $this->groupService->assign($this->twofaccountOne->id);
+        Groups::assign($this->twofaccountOne->id);
         
         $this->assertDatabaseHas('twofaccounts', [
             'id' => $this->twofaccountOne->id,
@@ -301,8 +294,8 @@ class GroupServiceTest extends FeatureTestCase
      */
     public function test_getAccounts_returns_accounts()
     {
-        $this->groupService->assign([$this->twofaccountOne->id, $this->twofaccountTwo->id], $this->groupOne);
-        $accounts = $this->groupService->getAccounts($this->groupOne);
+        Groups::assign([$this->twofaccountOne->id, $this->twofaccountTwo->id], $this->groupOne);
+        $accounts = Groups::getAccounts($this->groupOne);
         
         $this->assertEquals(2, $accounts->count());
     }

+ 7 - 16
tests/Unit/Api/v1/Controllers/GroupControllerTest.php

@@ -5,7 +5,7 @@ namespace Tests\Unit\Api\v1\Controllers;
 use App\Models\Group;
 use Tests\TestCase;
 use App\Models\TwoFAccount;
-use App\Services\GroupService;
+use App\Facades\Groups;
 use App\Services\SettingService;
 use Illuminate\Foundation\Testing\WithoutMiddleware;
 use App\Api\v1\Controllers\GroupController;
@@ -19,12 +19,6 @@ class GroupControllerTest extends TestCase
 {
     use WithoutMiddleware;
 
-    /**
-     * @var \Mockery\Mock|\App\Services\GroupService
-     */
-    protected $groupServiceMock;
-
-
     /**
      * @var \App\Api\v1\Controllers\GroupController tested controller
      */
@@ -41,9 +35,6 @@ class GroupControllerTest extends TestCase
     {
         parent::setUp();
 
-        $this->groupServiceMock = $this->mock(GroupService::class);
-
-        // $this->groupServiceMock = Mockery::mock($this->app->make(GroupService::class));
         $this->groupStoreRequest = Mockery::mock('App\Api\v1\Requests\GroupStoreRequest');
 
         $this->controller = new GroupController();
@@ -57,7 +48,7 @@ class GroupControllerTest extends TestCase
     {
         $groups = Group::factory()->count(3)->make();
 
-        $this->groupServiceMock->shouldReceive('getAll')
+        Groups::shouldReceive('getAll')
             ->once()
             ->andReturn($groups);
 
@@ -78,7 +69,7 @@ class GroupControllerTest extends TestCase
             ->once()
             ->andReturn(['name' => $group->name]);
 
-        $this->groupServiceMock->shouldReceive('create')
+        Groups::shouldReceive('create')
             ->once()
             ->andReturn($group);
 
@@ -112,7 +103,7 @@ class GroupControllerTest extends TestCase
             ->once()
             ->andReturn(['name' => $group->name]);
 
-        $this->groupServiceMock->shouldReceive('update')
+        Groups::shouldReceive('update')
             ->once()
             ->andReturn($group);
 
@@ -134,7 +125,7 @@ class GroupControllerTest extends TestCase
             ->once()
             ->andReturn(['ids' => $group->id]);
 
-        $this->groupServiceMock->shouldReceive('assign')
+        Groups::shouldReceive('assign')
             ->with($group->id, $group)
             ->once();
 
@@ -159,7 +150,7 @@ class GroupControllerTest extends TestCase
 
         $twofaccounts = TwoFAccount::factory()->count(3)->make();
 
-        $this->groupServiceMock->shouldReceive('getAccounts')
+        Groups::shouldReceive('getAccounts')
             ->with($group)
             ->once()
             ->andReturn($twofaccounts);
@@ -177,7 +168,7 @@ class GroupControllerTest extends TestCase
     {
         $group = Group::factory()->make();
 
-        $this->groupServiceMock->shouldReceive('delete')
+        Groups::shouldReceive('delete')
             ->once()
             ->with($group->id);