Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CreateFundCashflowRequestAction | |
0.00% |
0 / 31 |
|
0.00% |
0 / 2 |
240 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __invoke | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
210 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Action\Fund; |
| 6 | |
| 7 | use App\Domain\Funds\Service\FundCashflowRequestService; |
| 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 CreateFundCashflowRequestAction |
| 14 | { |
| 15 | public function __construct( |
| 16 | private FundCashflowRequestService $service, |
| 17 | private JsonRenderer $renderer, |
| 18 | ) {} |
| 19 | |
| 20 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
| 21 | { |
| 22 | $body = (array)$request->getParsedBody(); |
| 23 | $userId = (int)$request->getAttribute('userId'); |
| 24 | |
| 25 | $requestType = isset($body['requestType']) ? (string)$body['requestType'] : ''; |
| 26 | $fundId = isset($body['fundId']) ? (string)$body['fundId'] : ''; |
| 27 | $amount = isset($body['amount']) ? (string)$body['amount'] : ''; |
| 28 | $reason = isset($body['reason']) ? trim((string)$body['reason']) : null; |
| 29 | $notes = isset($body['notes']) ? trim((string)$body['notes']) : null; |
| 30 | |
| 31 | if ($requestType === '' || $fundId === '' || $amount === '') { |
| 32 | throw new InvalidArgumentException('requestType, fundId, and amount are required'); |
| 33 | } |
| 34 | |
| 35 | if ($requestType === 'add_funds') { |
| 36 | $created = $this->service->createAddFundsRequest( |
| 37 | userId: $userId, |
| 38 | fundId: $fundId, |
| 39 | amount: $amount, |
| 40 | notes: $notes !== '' ? $notes : null, |
| 41 | ); |
| 42 | } elseif ($requestType === 'withdrawal') { |
| 43 | $created = $this->service->createWithdrawalRequest( |
| 44 | userId: $userId, |
| 45 | fundId: $fundId, |
| 46 | amount: $amount, |
| 47 | reason: $reason !== '' ? $reason : null, |
| 48 | notes: $notes !== '' ? $notes : null, |
| 49 | ); |
| 50 | } else { |
| 51 | throw new InvalidArgumentException('Invalid requestType'); |
| 52 | } |
| 53 | |
| 54 | return $this->renderer->json($response, [ |
| 55 | 'success' => true, |
| 56 | 'message' => 'Request submitted successfully', |
| 57 | 'data' => $created->toArray(), |
| 58 | ], 201); |
| 59 | } |
| 60 | } |
| 61 |