Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Domain/EventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class EventStore extends ArrayCollection
{
Expand Down Expand Up @@ -89,13 +90,48 @@
$request = $this->requestStack->getCurrentRequest();
if ($request) {
$metadata['client_ip'] = $request->getClientIp();

if ($request->headers->has('User-Agent')) {
$metadata['user_agent'] = $request->headers->get('User-Agent');
} else {
$metadata['user_agent'] = 'unknown';
}

$metadata['route'] = $request->attributes->get('_route');
}

$user = $this->getUser();
if ($user) {
$metadata['username'] = $user->getUsername();
$metadata['role'] = $this->getRole($user);
}

return $metadata;
}

private function getRole(UserInterface $user): ?string

Check failure on line 112 in src/Domain/EventStore.php

View workflow job for this annotation

GitHub Actions / Lint PHP (8.4)

Method AppBundle\Domain\EventStore::getRole() never returns null so it can be removed from the return type.

Check failure on line 112 in src/Domain/EventStore.php

View workflow job for this annotation

GitHub Actions / Lint PHP (8.3)

Method AppBundle\Domain\EventStore::getRole() never returns null so it can be removed from the return type.
{
// Define roles priority for searching
$rolesPriority = [
'ROLE_ADMIN',
'ROLE_DISPATCHER',
'ROLE_COURIER',
'ROLE_RESTAURANT',
'ROLE_STORE',
];

$userRoles = $user->getRoles();

foreach ($rolesPriority as $role) {
if (in_array($role, $userRoles, true)) {
return $role;
}
}

if (count($userRoles) > 0) {
return $userRoles[0];
} else {
return 'ROLE_USER';
}
}
}
4 changes: 4 additions & 0 deletions src/Log/MessengerRouteProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
use Monolog\Attribute\AsMonologProcessor;
use Monolog\LogRecord;

/**
* This processor adds the route and controller information to the log records coming from the Messenger.
* Similarly to what @see \Symfony\Bridge\Monolog\Processor\RouteProcessor does for the web requests.
*/
#[AsMonologProcessor]
class MessengerRouteProcessor extends MessengerStampProcessor
{
Expand Down
Loading