Completed
Push — master ( 9c95f5...e428dc )
by Paweł
06:06
created

ScopeContextSubscriber::onRequest()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Settings Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\SettingsBundle\EventSubscriber;
18
19
use SWP\Bundle\SettingsBundle\Context\ScopeContextInterface;
20
use SWP\Bundle\SettingsBundle\Model\SettingsOwnerInterface;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
23
use Symfony\Component\HttpKernel\KernelEvents;
24
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
25
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
26
use Symfony\Component\Security\Core\User\UserInterface;
27
28
class ScopeContextSubscriber implements EventSubscriberInterface
29
{
30
    /**
31
     * @var ScopeContextInterface
32
     */
33
    protected $scopeContext;
34
35
    /**
36
     * @var TokenStorageInterface
37
     */
38
    protected $securityTokenStorage;
39
40
    /**
41
     * ScopeContextSubscriber constructor.
42
     *
43
     * @param ScopeContextInterface $scopeContext
44
     * @param TokenStorageInterface $tokenStorage
45
     */
46
    public function __construct(ScopeContextInterface $scopeContext, TokenStorageInterface $tokenStorage)
47
    {
48
        $this->scopeContext = $scopeContext;
49
        $this->securityTokenStorage = $tokenStorage;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public static function getSubscribedEvents()
56
    {
57
        return [
58
            KernelEvents::REQUEST => 'onRequest',
59
        ];
60
    }
61
62
    /**
63
     * @param GetResponseEvent $event
64
     */
65
    public function onRequest(GetResponseEvent $event)
66
    {
67
        if (!$event->isMasterRequest()) {
68
            return;
69
        }
70
71
        $token = $this->securityTokenStorage->getToken();
72
        if ($token instanceof TokenInterface) {
73
            $user = $token->getUser();
74
            if ($user instanceof UserInterface && $user instanceof SettingsOwnerInterface) {
75
                $this->scopeContext->setScopeOwner(ScopeContextInterface::SCOPE_USER, $user);
76
            }
77
        }
78
    }
79
}
80