Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
34 / 34 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CreateAccountAction | |
100.00% |
34 / 34 |
|
100.00% |
2 / 2 |
2 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
32 / 32 |
|
100.00% |
1 / 1 |
1 | |||
| 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 App\Support\Row; |
| 10 | use Psr\Http\Message\ResponseInterface; |
| 11 | use Psr\Http\Message\ServerRequestInterface; |
| 12 | |
| 13 | final class CreateAccountAction |
| 14 | { |
| 15 | private AccountService $accountService; |
| 16 | |
| 17 | private JsonRenderer $renderer; |
| 18 | |
| 19 | public function __construct( |
| 20 | AccountService $accountService, |
| 21 | JsonRenderer $renderer, |
| 22 | ) { |
| 23 | $this->accountService = $accountService; |
| 24 | $this->renderer = $renderer; |
| 25 | } |
| 26 | |
| 27 | public function __invoke( |
| 28 | ServerRequestInterface $request, |
| 29 | ResponseInterface $response, |
| 30 | ): ResponseInterface { |
| 31 | $data = (array)$request->getParsedBody(); |
| 32 | |
| 33 | // Accept both camelCase and snake_case for backwards compatibility |
| 34 | $investorId = Row::nullableInt($data, 'investorId') ?? 0; |
| 35 | $initialBalance = Row::nullableFloat($data, 'initialBalance') ?? 0.0; |
| 36 | $interestRate = Row::nullableFloat($data, 'interestRate'); |
| 37 | $loanToValueRatio = Row::nullableFloat($data, 'loanToValueRatio') ?? 0.80; |
| 38 | $account = $this->accountService->createAccountForInvestor( |
| 39 | $investorId, |
| 40 | $initialBalance, |
| 41 | $interestRate, |
| 42 | $loanToValueRatio, |
| 43 | ); |
| 44 | |
| 45 | // Convert string values to float for JSON output |
| 46 | return $this->renderer->json($response, [ |
| 47 | 'success' => true, |
| 48 | 'message' => 'Account created successfully', |
| 49 | 'data' => [ |
| 50 | 'accountId' => $account->accountId, |
| 51 | 'investorId' => $account->investorId, |
| 52 | 'accountNumber' => $account->accountNumber, |
| 53 | 'balance' => (float)$account->balance, |
| 54 | 'availableBalance' => (float)$account->availableBalance, |
| 55 | 'availableForLoan' => (float)$account->availableForLoan, |
| 56 | 'interestRate' => (float)$account->interestRate, |
| 57 | 'loanToValueRatio' => (float)$account->loanToValueRatio, |
| 58 | 'currency' => $account->currency, |
| 59 | 'status' => $account->status, |
| 60 | 'bankAccountId' => $account->bankAccountId, |
| 61 | 'bankAccountStatus' => $account->bankAccountStatus, |
| 62 | 'openedDate' => $account->openedDate, |
| 63 | 'createdAt' => $account->createdAt, |
| 64 | 'updatedAt' => $account->updatedAt, |
| 65 | ], |
| 66 | ], 201); |
| 67 | } |
| 68 | } |