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