Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ResolveErrorLogAction
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 2
56
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 / 22
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3declare(strict_types=1);
4
5namespace App\Action\SuperAdmin;
6
7use App\Domain\ErrorLog\Service\ErrorLogService;
8use App\Domain\Exception\AuthenticationException;
9use App\Domain\Exception\BadRequestException;
10use App\Domain\Exception\ConflictException;
11use App\Domain\Exception\NotFoundException;
12use App\Renderer\JsonRenderer;
13use App\Support\Row;
14use Psr\Http\Message\ResponseInterface;
15use Psr\Http\Message\ServerRequestInterface;
16use RuntimeException;
17
18/**
19 * Mark an error log as resolved.
20 */
21final readonly class ResolveErrorLogAction
22{
23    public function __construct(
24        private ErrorLogService $errorLogService,
25        private JsonRenderer $renderer
26    ) {}
27
28    /**
29     * @param array<string, string> $args
30     * @param ServerRequestInterface $request
31     * @param ResponseInterface $response
32     */
33    public function __invoke(
34        ServerRequestInterface $request,
35        ResponseInterface $response,
36        array $args
37    ): ResponseInterface {
38        $errorId = (int)$args['id'];
39        $data = (array)$request->getParsedBody();
40
41        // Get current user from JWT
42        $userId = Row::nullableInt($request->getAttributes(), 'userId');
43        if ($userId === null) {
44            throw new AuthenticationException('User not authenticated');
45        }
46
47        // Validate resolution notes
48        $notes = trim(Row::nullableString($data, 'notes') ?? '');
49        if ($notes === '') {
50            throw new BadRequestException('Resolution notes are required');
51        }
52
53        // Check if error exists
54        $error = $this->errorLogService->getById($errorId);
55        if ($error === null) {
56            throw new NotFoundException("Error log with ID {$errorId} not found");
57        }
58
59        if ($error->isResolved) {
60            throw new ConflictException('Error is already resolved');
61        }
62
63        $resolved = $this->errorLogService->resolve($errorId, $userId, $notes);
64
65        if (!$resolved) {
66            throw new RuntimeException('Failed to resolve error');
67        }
68
69        // Return the updated error
70        $updatedError = $this->errorLogService->getById($errorId);
71
72        return $this->renderer->json($response, [
73            'success' => true,
74            'message' => 'Error resolved successfully',
75            'data' => $updatedError,
76        ]);
77    }
78}