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

BlameSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 4
dl 0
loc 9
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 Doctrine\ORM\EntityManagerInterface;
17
use Gedmo\Blameable\BlameableListener;
18
use Symfony\Component\DependencyInjection\Attribute\Autowire;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\HttpFoundation\RequestStack;
21
use Symfony\Component\HttpKernel\Event\RequestEvent;
22
use Symfony\Component\HttpKernel\KernelEvents;
23
use Zikula\UsersBundle\UsersConstant;
24
25
/**
26
 * Class BlameSubscriber overrides Stof\DoctrineExtensionsBundle\EventListener\BlameListener
27
 */
28
class BlameSubscriber implements EventSubscriberInterface
29
{
30
    private bool $installed;
31
32
    public function __construct(
33
        #[Autowire(service: 'stof_doctrine_extensions.listener.blameable')]
34
        private readonly BlameableListener $blameableListener,
35
        private readonly EntityManagerInterface $entityManager,
36
        private readonly RequestStack $requestStack,
37
        #[Autowire('%env(ZIKULA_INSTALLED)%')]
38
        string $installed
39
    ) {
40
        $this->installed = '0.0.0' !== $installed;
41
    }
42
43
    public static function getSubscribedEvents(): array
44
    {
45
        return [
46
            KernelEvents::REQUEST => 'onKernelRequest',
47
        ];
48
    }
49
50
    public function onKernelRequest(RequestEvent $event): void
51
    {
52
        try {
53
            $uid = UsersConstant::USER_ID_ANONYMOUS;
54
            if (!$this->installed) {
55
                $uid = UsersConstant::USER_ID_ADMIN;
56
            } else {
57
                $request = $this->requestStack->getCurrentRequest();
58
                if (null !== $request && $request->hasSession() && ($session = $request->getSession())) {
0 ignored issues
show
Unused Code introduced by
The assignment to $session is dead and can be removed.
Loading history...
59
                    $uid = $this->session->isStarted() ? $this->session->get('uid', UsersConstant::USER_ID_ANONYMOUS) : $uid;
0 ignored issues
show
Bug Best Practice introduced by
The property session does not exist on Zikula\CoreBundle\EventSubscriber\BlameSubscriber. Did you maybe forget to declare it?
Loading history...
60
                }
61
            }
62
            $user = $this->entityManager->getReference('ZikulaUsersModule:UserEntity', $uid);
63
            $this->blameableListener->setUserValue($user);
64
        } catch (\Exception) {
65
            // silently fail - likely installing and tables not available
66
        }
67
    }
68
}
69