Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CreateAdminTransactionAction
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
72
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 / 28
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3declare(strict_types=1);
4
5namespace App\Action\Admin;
6
7use App\Domain\Transaction\Service\TransactionService;
8use App\Renderer\JsonRenderer;
9use InvalidArgumentException;
10use Psr\Http\Message\ResponseInterface;
11use Psr\Http\Message\ServerRequestInterface;
12
13final readonly class CreateAdminTransactionAction
14{
15    public function __construct(
16        private JsonRenderer $renderer,
17        private TransactionService $transactionService,
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['accountId'];
32        $body = (array)$request->getParsedBody();
33
34        // Validate required fields
35        $transactionType = $body['transactionType'] ?? null;
36        $amount = $body['amount'] ?? null;
37        $description = $body['description'] ?? null;
38
39        if ($transactionType === null || $transactionType === '') {
40            throw new InvalidArgumentException('Transaction type is required');
41        }
42
43        if ($amount === null || $amount === '') {
44            throw new InvalidArgumentException('Amount is required');
45        }
46
47        // Validate transaction type
48        $validTypes = ['deposit', 'withdrawal', 'interest', 'fee'];
49        if (!in_array($transactionType, $validTypes, true)) {
50            throw new InvalidArgumentException(
51                'Invalid transaction type. Must be one of: ' . implode(', ', $validTypes)
52            );
53        }
54
55        // Validate amount is positive
56        $amountFloat = (float)$amount;
57        if ($amountFloat <= 0) {
58            throw new InvalidArgumentException('Amount must be greater than zero');
59        }
60
61        // Create the transaction
62        $transaction = $this->transactionService->createTransaction(
63            accountId: $accountId,
64            transactionType: $transactionType,
65            amount: $amount,
66            description: $description,
67        );
68
69        return $this->renderer->json($response, [
70            'success' => true,
71            'message' => ucfirst($transactionType) . ' recorded successfully',
72            'data' => $transaction,
73        ], 201);
74    }
75}