requestStack->getSession(); if ($session instanceof SessionInterface) { $profileId = $session->get('profileId'); if ($profileId) { return (string) $profileId; } } } catch (Throwable) { // No session available (CLI context, etc.) } $user = $this->security->getUser(); if ($user instanceof Profile) { return $user->getId(); } return null; } }