Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| UpdateAccountStatusAction | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Action\Account; |
| 6 | |
| 7 | use App\Domain\Account\Service\AccountService; |
| 8 | use App\Renderer\JsonRenderer; |
| 9 | use InvalidArgumentException; |
| 10 | use Psr\Http\Message\ResponseInterface; |
| 11 | use Psr\Http\Message\ServerRequestInterface; |
| 12 | |
| 13 | final readonly class UpdateAccountStatusAction |
| 14 | { |
| 15 | public function __construct( |
| 16 | private AccountService $accountService, |
| 17 | private JsonRenderer $renderer, |
| 18 | ) {} |
| 19 | |
| 20 | /** |
| 21 | * @param ServerRequestInterface $request |
| 22 | * @param ResponseInterface $response |
| 23 | * @param array<string, string> $args |
| 24 | * @return ResponseInterface |
| 25 | */ |
| 26 | public function __invoke( |
| 27 | ServerRequestInterface $request, |
| 28 | ResponseInterface $response, |
| 29 | array $args, |
| 30 | ): ResponseInterface { |
| 31 | $accountId = (int)$args['id']; |
| 32 | $data = (array)$request->getParsedBody(); |
| 33 | $action = $data['action'] ?? ''; |
| 34 | |
| 35 | $account = match ($action) { |
| 36 | 'activate' => $this->accountService->activateAccount($accountId), |
| 37 | 'freeze' => $this->accountService->freezeAccount($accountId), |
| 38 | 'unfreeze' => $this->accountService->unfreezeAccount($accountId), |
| 39 | 'close' => $this->accountService->closeAccount($accountId), |
| 40 | default => throw new InvalidArgumentException( |
| 41 | 'Invalid action. Must be: activate, freeze, unfreeze, or close', |
| 42 | ), |
| 43 | }; |
| 44 | |
| 45 | return $this->renderer->json($response, [ |
| 46 | 'success' => true, |
| 47 | 'message' => ucfirst($action) . ' successful', |
| 48 | 'data' => [ |
| 49 | 'accountId' => $account->accountId, |
| 50 | 'status' => $account->status, |
| 51 | 'updatedAt' => $account->updatedAt, |
| 52 | ], |
| 53 | ]); |
| 54 | } |
| 55 | } |