Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DeleteTestDataAction
100.00% covered (success)
100.00%
11 / 11
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%
10 / 10
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 Psr\Http\Message\ResponseInterface;
12use Psr\Http\Message\ServerRequestInterface;
13
14/**
15 * Delete all test data (users, investors, accounts, transactions with @testdata.local emails).
16 */
17final readonly class DeleteTestDataAction
18{
19    public function __construct(
20        private JsonRenderer $renderer,
21        private SuperAdminService $service,
22    ) {}
23
24    public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
25    {
26        // Verify super_admin role
27        $user = $request->getAttribute('user');
28        if (!$user instanceof UserAuthData || $user->role !== 'super_admin') {
29            throw new ForbiddenException('Super admin access required');
30        }
31
32        $deleted = $this->service->deleteTestData();
33
34        $total = $deleted['users'] + $deleted['investors'] + $deleted['accounts'] + $deleted['transactions'];
35
36        return $this->renderer->json($response, [
37            'success' => true,
38            'message' => "Deleted {$total} test data records",
39            'data' => $deleted,
40        ]);
41    }
42}