diff --git a/app/Api/v1/Controllers/GroupController.php b/app/Api/v1/Controllers/GroupController.php index 13a3aba4..7b201c94 100644 --- a/app/Api/v1/Controllers/GroupController.php +++ b/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 @@ 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 @@ public function __construct() */ public function index() { - $groups = $this->groupService->getAll(); + $groups = Groups::getAll(); return GroupResource::collection($groups); } @@ -53,7 +37,7 @@ public function store(GroupStoreRequest $request) { $validated = $request->validated(); - $group = $this->groupService->create($validated); + $group = Groups::create($validated); return (new GroupResource($group)) ->response() @@ -84,7 +68,7 @@ public function update(GroupStoreRequest $request, Group $group) { $validated = $request->validated(); - $this->groupService->update($group, $validated); + Groups::update($group, $validated); return new GroupResource($group); @@ -102,7 +86,7 @@ public function assignAccounts(GroupAssignRequest $request, Group $group) { $validated = $request->validated(); - $this->groupService->assign($validated['ids'], $group); + Groups::assign($validated['ids'], $group); return new GroupResource($group); @@ -117,7 +101,7 @@ public function assignAccounts(GroupAssignRequest $request, Group $group) */ public function accounts(Group $group) { - $twofaccounts = $this->groupService->getAccounts($group); + $twofaccounts = Groups::getAccounts($group); return new TwoFAccountCollection($twofaccounts); @@ -132,7 +116,7 @@ public function accounts(Group $group) */ public function destroy(Group $group) { - $this->groupService->delete($group->id); + Groups::delete($group->id); return response()->json(null, 204); } diff --git a/app/Api/v1/Controllers/TwoFAccountController.php b/app/Api/v1/Controllers/TwoFAccountController.php index cf62f31f..ac6ff0fc 100644 --- a/app/Api/v1/Controllers/TwoFAccountController.php +++ b/app/Api/v1/Controllers/TwoFAccountController.php @@ -13,7 +13,7 @@ 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 @@ public function store(TwoFAccountDynamicRequest $request) $twofaccount->save(); // Possible group association - $this->groupService->assign($twofaccount->id); + Groups::assign($twofaccount->id); return (new TwoFAccountReadResource($twofaccount->refresh())) ->response() diff --git a/app/Facades/Groups.php b/app/Facades/Groups.php new file mode 100644 index 00000000..dd10fece --- /dev/null +++ b/app/Facades/Groups.php @@ -0,0 +1,14 @@ +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 @@ public function boot() public function provides() { return [ - GroupService::class, LogoService::class, TwoFAccountService::class, ]; diff --git a/app/Services/GroupService.php b/app/Services/GroupService.php index 795ada1d..36134f75 100644 --- a/app/Services/GroupService.php +++ b/app/Services/GroupService.php @@ -7,32 +7,16 @@ 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 @@ public function getAll() : Collection * @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 @@ public function create(array $data) : Group * @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 @@ public function update(Group $group, array $data) : Group * @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 @@ public function delete($ids) : int * @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 @@ public function assign($ids, Group $group = null) : void * @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 @@ public function getAccounts(Group $group) : Collection * * @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); } diff --git a/tests/Feature/Services/GroupServiceTest.php b/tests/Feature/Services/GroupServiceTest.php index d9f2f24b..6a8c55ca 100644 --- a/tests/Feature/Services/GroupServiceTest.php +++ b/tests/Feature/Services/GroupServiceTest.php @@ -5,7 +5,7 @@ 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 @@ */ class GroupServiceTest extends FeatureTestCase { - /** - * App\Services\GroupService $groupService - */ - protected $groupService; - - /** * App\Services\SettingService $settingService */ @@ -58,7 +52,6 @@ public function setUp() : void { parent::setUp(); - $this->groupService = $this->app->make(GroupService::class); $this->settingService = $this->app->make(SettingService::class); $this->groupOne = new Group; @@ -102,7 +95,7 @@ public function setUp() : void */ 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 @@ public function test_getAll_returns_a_collection() */ 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 @@ public function test_getAll_adds_pseudo_group_on_top_of_user_groups() */ 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 @@ public function test_getAll_returns_pseudo_group_with_all_twofaccounts_count() */ 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 @@ public function test_create_persists_and_returns_created_group() */ 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 @@ public function test_update_persists_and_returns_updated_group() */ 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 @@ public function test_delete_a_groupId_clear_db_and_returns_deleted_count() */ 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 @@ public function test_delete_default_group_reset_defaultGroup_setting() { $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 @@ public function test_delete_active_group_reset_activeGroup_setting() $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 @@ public function test_delete_active_group_reset_activeGroup_setting() 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 @@ public function test_assign_a_twofaccountid_to_a_specified_group_persists_the_re */ 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 @@ public function test_assign_a_twofaccountid_to_no_group_assigns_to_default_group { $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 @@ public function test_assign_a_twofaccountid_to_no_group_assigns_to_active_group( $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 @@ public function test_assign_a_twofaccountid_to_missing_active_group_does_not_fai $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 @@ public function test_assign_a_twofaccountid_to_missing_active_group_does_not_fai */ 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()); } diff --git a/tests/Unit/Api/v1/Controllers/GroupControllerTest.php b/tests/Unit/Api/v1/Controllers/GroupControllerTest.php index 6e308764..cc23cf79 100644 --- a/tests/Unit/Api/v1/Controllers/GroupControllerTest.php +++ b/tests/Unit/Api/v1/Controllers/GroupControllerTest.php @@ -5,7 +5,7 @@ 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 @@ public function setUp() : void { 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 @@ public function test_index_returns_api_resources_using_groupService() { $groups = Group::factory()->count(3)->make(); - $this->groupServiceMock->shouldReceive('getAll') + Groups::shouldReceive('getAll') ->once() ->andReturn($groups); @@ -78,7 +69,7 @@ public function test_store_returns_api_resource_stored_using_groupService() ->once() ->andReturn(['name' => $group->name]); - $this->groupServiceMock->shouldReceive('create') + Groups::shouldReceive('create') ->once() ->andReturn($group); @@ -112,7 +103,7 @@ public function test_update_returns_api_resource_updated_using_groupService() ->once() ->andReturn(['name' => $group->name]); - $this->groupServiceMock->shouldReceive('update') + Groups::shouldReceive('update') ->once() ->andReturn($group); @@ -134,7 +125,7 @@ public function test_assignAccounts_returns_api_resource_assigned_using_groupSer ->once() ->andReturn(['ids' => $group->id]); - $this->groupServiceMock->shouldReceive('assign') + Groups::shouldReceive('assign') ->with($group->id, $group) ->once(); @@ -159,7 +150,7 @@ public function test_accounts_returns_api_resources_fetched_using_groupService() $twofaccounts = TwoFAccount::factory()->count(3)->make(); - $this->groupServiceMock->shouldReceive('getAccounts') + Groups::shouldReceive('getAccounts') ->with($group) ->once() ->andReturn($twofaccounts); @@ -177,7 +168,7 @@ public function test_destroy_uses_group_service() { $group = Group::factory()->make(); - $this->groupServiceMock->shouldReceive('delete') + Groups::shouldReceive('delete') ->once() ->with($group->id);