Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CreateTestUserAction
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace App\Action\SuperAdmin;
6
7use App\Domain\Auth\Data\UserAuthData;
8use App\Domain\Exception\ForbiddenException;
9use App\Domain\SuperAdmin\Service\SuperAdminService;
10use App\Renderer\JsonRenderer;
11use App\Support\Row;
12use Psr\Http\Message\ResponseInterface;
13use Psr\Http\Message\ServerRequestInterface;
14
15/**
16 * Create a single test user with investor profile and optional account.
17 */
18final readonly class CreateTestUserAction
19{
20    public function __construct(
21        private JsonRenderer $renderer,
22        private SuperAdminService $service,
23    ) {}
24
25    public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
26    {
27        // Verify super_admin role
28        $user = $request->getAttribute('user');
29        if (!$user instanceof UserAuthData || $user->role !== 'super_admin') {
30            throw new ForbiddenException('Super admin access required');
31        }
32
33        $data = (array)$request->getParsedBody();
34
35        $result = $this->service->createTestUser(
36            firstName: Row::nullableString($data, 'firstName') ?? '',
37            lastName: Row::nullableString($data, 'lastName') ?? '',
38            email: Row::nullableString($data, 'email'),
39            password: Row::nullableString($data, 'password'),
40            kycStatus: Row::nullableString($data, 'kycStatus') ?? 'verified',
41            createAccount: Row::nullableBool($data, 'createAccount') ?? true,
42            initialInvestment: Row::nullableFloat($data, 'initialInvestment'),
43        );
44
45        return $this->renderer->json($response, [
46            'success' => true,
47            'message' => 'Test user created successfully',
48            'data' => $result,
49        ])->withStatus(201);
50    }
51}