Skip to content

Commit 8450f13

Browse files
committed
Small optimizations
Signed-off-by: alexmerlin <[email protected]>
1 parent 3ef4137 commit 8450f13

File tree

12 files changed

+87
-43
lines changed

12 files changed

+87
-43
lines changed

src/Admin/src/Handler/Admin/DeleteAdminResourceHandler.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
namespace Api\Admin\Handler\Admin;
66

77
use Api\App\Handler\AbstractHandler;
8-
use Core\Admin\Entity\Admin;
98
use Core\Admin\Service\AdminServiceInterface;
109
use Core\App\Exception\NotFoundException;
11-
use Core\App\Message;
1210
use Dot\DependencyInjection\Attribute\Inject;
1311
use Psr\Http\Message\ResponseInterface;
1412
use Psr\Http\Message\ServerRequestInterface;
@@ -28,12 +26,9 @@ public function __construct(
2826
*/
2927
public function handle(ServerRequestInterface $request): ResponseInterface
3028
{
31-
$admin = $this->adminService->getAdminRepository()->find($request->getAttribute('uuid'));
32-
if (! $admin instanceof Admin) {
33-
throw new NotFoundException(Message::ADMIN_NOT_FOUND);
34-
}
35-
36-
$this->adminService->getAdminRepository()->deleteAdmin($admin);
29+
$this->adminService->getAdminRepository()->deleteAdmin(
30+
$this->adminService->find($request->getAttribute('uuid'))
31+
);
3732

3833
return $this->noContentResponse();
3934
}

src/Admin/src/Handler/Admin/GetAdminResourceHandler.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
namespace Api\Admin\Handler\Admin;
66

77
use Api\App\Handler\AbstractHandler;
8-
use Core\Admin\Entity\Admin;
98
use Core\Admin\Service\AdminServiceInterface;
109
use Core\App\Exception\NotFoundException;
11-
use Core\App\Message;
1210
use Dot\DependencyInjection\Attribute\Inject;
1311
use Psr\Http\Message\ResponseInterface;
1412
use Psr\Http\Message\ServerRequestInterface;
@@ -28,11 +26,9 @@ public function __construct(
2826
*/
2927
public function handle(ServerRequestInterface $request): ResponseInterface
3028
{
31-
$admin = $this->adminService->getAdminRepository()->find($request->getAttribute('uuid'));
32-
if (! $admin instanceof Admin) {
33-
throw new NotFoundException(Message::ADMIN_NOT_FOUND);
34-
}
35-
36-
return $this->createResponse($request, $admin);
29+
return $this->createResponse(
30+
$request,
31+
$this->adminService->find($request->getAttribute('uuid'))
32+
);
3733
}
3834
}

src/Admin/src/Handler/Admin/PatchAdminResourceHandler.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66

77
use Api\Admin\InputFilter\UpdateAdminInputFilter;
88
use Api\App\Handler\AbstractHandler;
9-
use Core\Admin\Entity\Admin;
109
use Core\Admin\Service\AdminServiceInterface;
1110
use Core\App\Exception\BadRequestException;
1211
use Core\App\Exception\ConflictException;
1312
use Core\App\Exception\NotFoundException;
14-
use Core\App\Message;
1513
use Dot\DependencyInjection\Attribute\Inject;
1614
use Psr\Http\Message\ResponseInterface;
1715
use Psr\Http\Message\ServerRequestInterface;
@@ -40,10 +38,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
4038
throw (new BadRequestException())->setMessages($this->inputFilter->getMessages());
4139
}
4240

43-
$admin = $this->adminService->getAdminRepository()->find($request->getAttribute('uuid'));
44-
if (! $admin instanceof Admin) {
45-
throw new NotFoundException(Message::ADMIN_NOT_FOUND);
46-
}
41+
$admin = $this->adminService->find($request->getAttribute('uuid'));
4742

4843
$this->adminService->updateAdmin($admin, (array) $this->inputFilter->getValues());
4944

src/Admin/src/Handler/Admin/Role/GetAdminRoleResourceHandler.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
namespace Api\Admin\Handler\Admin\Role;
66

77
use Api\App\Handler\AbstractHandler;
8-
use Core\Admin\Entity\AdminRole;
98
use Core\Admin\Service\AdminRoleServiceInterface;
109
use Core\App\Exception\NotFoundException;
11-
use Core\App\Message;
1210
use Dot\DependencyInjection\Attribute\Inject;
1311
use Psr\Http\Message\ResponseInterface;
1412
use Psr\Http\Message\ServerRequestInterface;
@@ -28,11 +26,9 @@ public function __construct(
2826
*/
2927
public function handle(ServerRequestInterface $request): ResponseInterface
3028
{
31-
$adminRole = $this->adminRoleService->getAdminRoleRepository()->find($request->getAttribute('uuid'));
32-
if (! $adminRole instanceof AdminRole) {
33-
throw new NotFoundException(Message::ROLE_NOT_FOUND);
34-
}
35-
36-
return $this->createResponse($request, $adminRole);
29+
return $this->createResponse(
30+
$request,
31+
$this->adminRoleService->find($request->getAttribute('uuid'))
32+
);
3733
}
3834
}

src/Core/src/Admin/src/Service/AdminRoleService.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace Core\Admin\Service;
66

7+
use Core\Admin\Entity\AdminRole;
78
use Core\Admin\Repository\AdminRoleRepository;
9+
use Core\App\Exception\NotFoundException;
10+
use Core\App\Message;
811
use Dot\DependencyInjection\Attribute\Inject;
912

1013
class AdminRoleService implements AdminRoleServiceInterface
@@ -21,4 +24,17 @@ public function getAdminRoleRepository(): AdminRoleRepository
2124
{
2225
return $this->adminRoleRepository;
2326
}
27+
28+
/**
29+
* @throws NotFoundException
30+
*/
31+
public function find(string $id): AdminRole
32+
{
33+
$adminRole = $this->adminRoleRepository->find($id);
34+
if (! $adminRole instanceof AdminRole) {
35+
throw new NotFoundException(Message::ROLE_NOT_FOUND);
36+
}
37+
38+
return $adminRole;
39+
}
2440
}

src/Core/src/Admin/src/Service/AdminRoleServiceInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44

55
namespace Core\Admin\Service;
66

7+
use Core\Admin\Entity\AdminRole;
78
use Core\Admin\Repository\AdminRoleRepository;
9+
use Core\App\Exception\NotFoundException;
810

911
interface AdminRoleServiceInterface
1012
{
1113
public function getAdminRoleRepository(): AdminRoleRepository;
14+
15+
/**
16+
* @throws NotFoundException
17+
*/
18+
public function find(string $id): AdminRole;
1219
}

src/Core/src/Admin/src/Service/AdminService.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ public function deleteAdmin(Admin $admin): void
6464
$this->adminRepository->deleteAdmin($admin);
6565
}
6666

67+
/**
68+
* @throws NotFoundException
69+
*/
70+
public function find(string $id): Admin
71+
{
72+
$admin = $this->adminRepository->find($id);
73+
if (! $admin instanceof Admin) {
74+
throw new NotFoundException(Message::ADMIN_NOT_FOUND);
75+
}
76+
77+
return $admin;
78+
}
79+
6780
/**
6881
* @throws BadRequestException
6982
* @throws ConflictException

src/Core/src/Admin/src/Service/AdminServiceInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public function createAdmin(array $data = []): Admin;
2222

2323
public function deleteAdmin(Admin $admin): void;
2424

25+
/**
26+
* @throws NotFoundException
27+
*/
28+
public function find(string $id): Admin;
29+
2530
/**
2631
* @throws BadRequestException
2732
* @throws ConflictException

src/Core/src/User/src/Service/UserRoleService.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Core\User\Service;
66

7+
use Core\App\Exception\NotFoundException;
8+
use Core\App\Message;
9+
use Core\User\Entity\UserRole;
710
use Core\User\Repository\UserRoleRepository;
811
use Dot\DependencyInjection\Attribute\Inject;
912

@@ -13,12 +16,25 @@ class UserRoleService implements UserRoleServiceInterface
1316
UserRoleRepository::class,
1417
)]
1518
public function __construct(
16-
protected UserRoleRepository $roleRepository,
19+
protected UserRoleRepository $userRoleRepository,
1720
) {
1821
}
1922

20-
public function getRoleRepository(): UserRoleRepository
23+
public function getUserRoleRepository(): UserRoleRepository
2124
{
22-
return $this->roleRepository;
25+
return $this->userRoleRepository;
26+
}
27+
28+
/**
29+
* @throws NotFoundException
30+
*/
31+
public function find(string $id): UserRole
32+
{
33+
$userRole = $this->userRoleRepository->find($id);
34+
if (! $userRole instanceof UserRole) {
35+
throw new NotFoundException(Message::ROLE_NOT_FOUND);
36+
}
37+
38+
return $userRole;
2339
}
2440
}

src/Core/src/User/src/Service/UserRoleServiceInterface.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44

55
namespace Core\User\Service;
66

7+
use Core\App\Exception\NotFoundException;
8+
use Core\User\Entity\UserRole;
79
use Core\User\Repository\UserRoleRepository;
810

911
interface UserRoleServiceInterface
1012
{
11-
public function getRoleRepository(): UserRoleRepository;
13+
public function getUserRoleRepository(): UserRoleRepository;
14+
15+
/**
16+
* @throws NotFoundException
17+
*/
18+
public function find(string $id): UserRole;
1219
}

0 commit comments

Comments
 (0)