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\NotFoundException;
9use App\Renderer\JsonRenderer;
10use DomainException;
11use InvalidArgumentException;
12use Psr\Http\Message\ResponseInterface;
13use Psr\Http\Message\ServerRequestInterface;
14
15/**
16 * Mark an error log as resolved.
17 */
18final readonly class ResolveErrorLogAction
19{
20    public function __construct(
21        private ErrorLogService $errorLogService,
22        private JsonRenderer $renderer
23    ) {}
24
25    /**
26     * @param array<string, string> $args
27     * @param ServerRequestInterface $request
28     * @param ResponseInterface $response
29     */
30    public function __invoke(
31        ServerRequestInterface $request,
32        ResponseInterface $response,
33        array $args
34    ): ResponseInterface {
35        $errorId = (int)$args['id'];
36        $data = (array)$request->getParsedBody();
37
38        // Get current user from JWT
39        $userId = $request->getAttribute('userId');
40        if ($userId === null) {
41            throw new DomainException('User not authenticated');
42        }
43
44        // Validate resolution notes
45        $notes = trim($data['notes'] ?? '');
46        if ($notes === '') {
47            throw new InvalidArgumentException('Resolution notes are required');
48        }
49
50        // Check if error exists
51        $error = $this->errorLogService->getById($errorId);
52        if ($error === null) {
53            throw new NotFoundException("Error log with ID {$errorId} not found");
54        }
55
56        if ($error->isResolved) {
57            throw new DomainException('Error is already resolved');
58        }
59
60        $resolved = $this->errorLogService->resolve($errorId, (int)$userId, $notes);
61
62        if (!$resolved) {
63            throw new DomainException('Failed to resolve error');
64        }
65
66        // Return the updated error
67        $updatedError = $this->errorLogService->getById($errorId);
68
69        return $this->renderer->json($response, [
70            'success' => true,
71            'message' => 'Error resolved successfully',
72            'data' => $updatedError,
73        ]);
74    }
75}