Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.47% covered (warning)
89.47%
17 / 19
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateKycStatusAction
89.47% covered (warning)
89.47%
17 / 19
50.00% covered (danger)
50.00%
1 / 2
4.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
3.01
1<?php
2
3declare(strict_types=1);
4
5namespace App\Action\Investor;
6
7use App\Domain\Exception\BadRequestException;
8use App\Domain\Exception\ValidationException;
9use App\Domain\Investor\Service\InvestorService;
10use App\Renderer\JsonRenderer;
11use App\Support\Row;
12use Psr\Http\Message\ResponseInterface;
13use Psr\Http\Message\ServerRequestInterface;
14
15/**
16 * Handles the action of updating an investor's KYC (Know Your Customer) status.
17 *
18 * This class is responsible for processing the HTTP request to update the KYC status
19 * of a specified investor. It validates the investor ID, the provided KYC status,
20 * and updates the status using the InvestorService. It also constructs the applicable
21 * JSON response using the JsonRenderer.
22 *
23 * Dependencies:
24 * - InvestorService: Provides methods to update and manage investor-related data.
25 * - JsonRenderer: Responsible for rendering JSON responses.
26 *
27 * Methods:
28 * - __construct: Initializes the action with required dependencies.
29 * - __invoke: Handles the incoming HTTP request, processes the update, and returns an HTTP response.
30 *
31 * Exceptions:
32 * - InvalidArgumentException: Thrown when invalid input (e.g., invalid ID, missing status) is provided.
33 */
34final readonly class UpdateKycStatusAction
35{
36    public function __construct(
37        private InvestorService $investorService,
38        private JsonRenderer $renderer,
39    ) {}
40
41    /**
42     * @param array<string, string> $args
43     * @param ServerRequestInterface $request
44     * @param ResponseInterface $response
45     */
46    public function __invoke(
47        ServerRequestInterface $request,
48        ResponseInterface $response,
49        array $args,
50    ): ResponseInterface {
51        // Get investor ID from route
52        $investorId = (int)$args['id'];
53
54        // Get KYC status from request body
55        $data = (array)$request->getParsedBody();
56        $status = Row::nullableString($data, 'status') ?? '';
57
58        // Validate investor ID
59        if ($investorId <= 0) {
60            throw new ValidationException('Invalid investor ID');
61        }
62
63        // Validate status is not empty
64        if ($status === '') {
65            throw new BadRequestException('Status is required');
66        }
67
68        // Update KYC status
69        $investor = $this->investorService->updateKycStatus($investorId, $status);
70
71        // Return updated investor data
72        return $this->renderer->json($response, [
73            'success' => true,
74            'message' => 'KYC status updated successfully',
75            'data' => [
76                'investorId' => $investor->investorId,
77                'kycStatus' => $investor->kycStatus,
78                'status' => $investor->status,
79                'updatedAt' => $investor->updatedAt,
80            ],
81        ]);
82    }
83}