getAuthenticatedParticipant()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 1
f 0
cc 3
eloc 5
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the MilioooMessageBundle package.
5
 *
6
 * (c) Michiel boeckaert <[email protected]>
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Miliooo\Messaging\User;
12
13
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
14
use Symfony\Component\Security\Core\SecurityContextInterface;
15
16
/**
17
 * Participant provider from the security context.
18
 *
19
 * Twig extension can't handle with the security token so we need a provider from the securityContext.
20
 * http://stackoverflow.com/questions/18770467/
21
 *
22
 * @author Michiel Boeckaert <[email protected]>
23
 */
24
class ParticipantProviderSecurityContext implements ParticipantProviderInterface
25
{
26
    protected $securityContext;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param SecurityContextInterface $securityContext A security context interface
32
     */
33
    public function __construct(SecurityContextInterface $securityContext)
34
    {
35
        $this->securityContext = $securityContext;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getAuthenticatedParticipant()
42
    {
43
        $participant = $this->securityContext->getToken()->getUser();
44
45
        if (!is_object($participant) || !$participant instanceof ParticipantInterface) {
46
            throw new AccessDeniedException('You must be logged in with a participant interface');
47
        }
48
49
        return $participant;
50
    }
51
}
52