Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SubmitFundingRequestAction
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __invoke
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3declare(strict_types=1);
4
5namespace App\Action\Funding;
6
7use App\Domain\Exception\BadRequestException;
8use App\Domain\Funding\Service\FundingRequestService;
9use App\Renderer\JsonRenderer;
10use App\Support\Row;
11use Psr\Http\Message\ResponseInterface;
12use Psr\Http\Message\ServerRequestInterface;
13
14/**
15 * POST /api/me/funding-requests
16 *
17 * Lets the authenticated investor submit a deposit or withdrawal request
18 * (FSC-21). No money moves — an admin processes it. Scoped to the caller's
19 * investorId from the JWT.
20 */
21final readonly class SubmitFundingRequestAction
22{
23    public function __construct(
24        private FundingRequestService $service,
25        private JsonRenderer $renderer,
26    ) {}
27
28    public function __invoke(
29        ServerRequestInterface $request,
30        ResponseInterface $response,
31    ): ResponseInterface {
32        $investorId = Row::nullableInt($request->getAttributes(), 'investorId');
33        if ($investorId === null) {
34            throw new BadRequestException('Only investors can submit funding requests');
35        }
36
37        $body = (array)$request->getParsedBody();
38        $type = Row::nullableString($body, 'type') ?? '';
39        $amount = Row::nullableString($body, 'amount') ?? '';
40        $note = Row::nullableString($body, 'note');
41
42        $created = $this->service->submit($investorId, $type, $amount, $note);
43
44        $message = $created->requestType === 'withdrawal'
45            ? 'Withdrawal request submitted — our team will be in touch.'
46            : 'Investment request submitted — our team will send funding instructions.';
47
48        return $this->renderer->json($response, [
49            'success' => true,
50            'message' => $message,
51            'data' => ['request' => $created->toArray()],
52        ]);
53    }
54}