• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemsdk / docker-symfony-api / #74

pending completion
#74

push

DKravtsov
Php 8.2, symfony 6.2, updated RabbitMQ, updated composer dependencies, refactoring.

51 of 51 new or added lines in 44 files covered. (100.0%)

1479 of 2668 relevant lines covered (55.43%)

23.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.07
/src/General/Transport/EventSubscriber/RequestLogSubscriber.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\General\Transport\EventSubscriber;
6

7
use App\ApiKey\Application\Security\ApiKeyUser;
8
use App\Log\Application\Service\RequestLoggerService;
9
use App\User\Application\Security\SecurityUser;
10
use App\User\Application\Security\UserTypeIdentification;
11
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Event\TerminateEvent;
14
use Throwable;
15

16
use function array_filter;
17
use function array_values;
18
use function in_array;
19
use function str_contains;
20
use function substr;
21

22
/**
23
 * Class RequestLogSubscriber
24
 *
25
 * @package App\General
26
 *
27
 * @property array<int, string> $ignoredRoutes
28
 */
29
class RequestLogSubscriber implements EventSubscriberInterface
30
{
31
    /**
32
     * Constructor
33
     *
34
     * @param array<int, string> $ignoredRoutes
35
     */
36
    public function __construct(
37
        private readonly RequestLoggerService $requestLoggerService,
38
        private readonly UserTypeIdentification $userService,
39
        private readonly array $ignoredRoutes,
40
    ) {
41
    }
145✔
42

43
    /**
44
     * {@inheritdoc}
45
     *
46
     * @return array<string, array<int, string|int>>
47
     */
48
    public static function getSubscribedEvents(): array
49
    {
50
        return [
×
51
            TerminateEvent::class => [
×
52
                'onTerminateEvent',
×
53
                15,
×
54
            ],
×
55
        ];
×
56
    }
57

58
    /**
59
     * Subscriber method to log every request / response.
60
     *
61
     * @throws Throwable
62
     */
63
    public function onTerminateEvent(TerminateEvent $event): void
64
    {
65
        $request = $event->getRequest();
145✔
66
        $path = $request->getPathInfo();
145✔
67

68
        $filter = static fn (string $route): bool =>
145✔
69
            str_contains($route, '/*') && str_contains($path, substr($route, 0, -2));
139✔
70

71
        // We don't want to log OPTIONS requests, /_profiler* -path, ignored routes and wildcard ignored routes
72
        if (
73
            $request->getRealMethod() === Request::METHOD_OPTIONS
145✔
74
            || str_contains($path, '/_profiler')
145✔
75
            || in_array($path, $this->ignoredRoutes, true)
145✔
76
            || array_values(array_filter($this->ignoredRoutes, $filter)) !== []
145✔
77
        ) {
78
            return;
6✔
79
        }
80

81
        $this->process($event);
139✔
82
    }
83

84
    /**
85
     * Method to process current request event.
86
     *
87
     * @throws Throwable
88
     */
89
    private function process(TerminateEvent $event): void
90
    {
91
        $request = $event->getRequest();
139✔
92
        // Set needed data to logger and handle actual log
93
        $this->requestLoggerService->setRequest($request);
139✔
94
        $this->requestLoggerService->setResponse($event->getResponse());
139✔
95
        $identify = $this->userService->getIdentity();
139✔
96

97
        if ($identify instanceof SecurityUser) {
139✔
98
            $this->requestLoggerService->setUserId($identify->getUserIdentifier());
126✔
99
        } elseif ($identify instanceof ApiKeyUser) {
13✔
100
            $this->requestLoggerService->setApiKeyId($identify->getApiKeyIdentifier());
×
101
        }
102

103
        $this->requestLoggerService->setMainRequest($event->isMainRequest());
139✔
104
        $this->requestLoggerService->handle();
139✔
105
    }
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc