Passed
Push — main ( c1e5b8...0797d2 )
by Axel
04:05
created

LoggableSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\CoreBundle\EventSubscriber;
15
16
use Gedmo\Loggable\LoggableListener as Loggable;
17
use Symfony\Bundle\SecurityBundle\Security;
18
use Symfony\Component\DependencyInjection\Attribute\Autowire;
19
use Symfony\Component\HttpKernel\Event\RequestEvent;
20
use Symfony\Component\HttpKernel\HttpKernelInterface;
21
use Symfony\Component\HttpKernel\KernelEvents;
22
use Symfony\Contracts\Translation\TranslatorInterface;
23
24
/**
25
 * Loggable subscriber to provide the current user. Note we use the ID to avoid storing user names.
26
 */
27
class LoggableSubscriber
28
{
29
    public function __construct(
30
        #[Autowire(service: 'stof_doctrine_extensions.listener.loggable')]
31
        private readonly Loggable $loggableListener,
32
        private readonly TranslatorInterface $translator,
33
        private readonly Security $security
34
    ) {
35
    }
36
37
    public static function getSubscribedEvents(): array
38
    {
39
        return [
40
            KernelEvents::REQUEST => 'onKernelRequest',
41
        ];
42
    }
43
44
    /**
45
     * Set the username from the current user api.
46
     */
47
    public function onKernelRequest(RequestEvent $event): void
48
    {
49
        if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
0 ignored issues
show
Deprecated Code introduced by
The constant Symfony\Component\HttpKe...terface::MASTER_REQUEST has been deprecated: since symfony/http-kernel 5.3, use MAIN_REQUEST instead. To ease the migration, this constant won't be removed until Symfony 7.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

49
        if (/** @scrutinizer ignore-deprecated */ HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
50
            return;
51
        }
52
53
        $this->loggableListener->setUsername($this->security->getUser()?->getId());
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Symfony\Component\Security\Core\User\UserInterface. It seems like you code against a sub-type of Symfony\Component\Security\Core\User\UserInterface such as Zikula\UsersBundle\Entity\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        $this->loggableListener->setUsername($this->security->getUser()?->/** @scrutinizer ignore-call */ getId());
Loading history...
54
    }
55
}
56