Completed
Pull Request — master (#32)
by Rafał
03:20
created

BaseController::hasPermission()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 * @author Rafał Muszyński <[email protected]>
5
 * @copyright 2015 Sourcefabric z.ú.
6
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
7
 */
8
9
namespace Newscoop\PaywallBundle\Controller;
10
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Symfony\Component\EventDispatcher\Event;
13
use Newscoop\EventDispatcher\Events\GenericEvent;
14
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
15
16
/**
17
 * Base Controller.
18
 */
19
abstract class BaseController extends Controller
20
{
21
    /**
22
     * Dispatch event.
23
     *
24
     * @param string $name
25
     * @param Event  $event
26
     */
27
    protected function dispatchEvent($name, Event $event)
28
    {
29
        $this->get('event_dispatcher')->dispatch($name, $event);
30
    }
31
32
    /**
33
     * Dispatch notification event.
34
     *
35
     * @param string $name
36
     * @param mixed  $subscription
37
     */
38
    protected function dispatchNotificationEvent($name, $subscription)
39
    {
40
        $this->dispatchEvent($name, new GenericEvent($subscription));
41
    }
42
43
    /**
44
     * Checks if user has permission.
45
     *
46
     * @param string $permission Permission name
47
     *
48
     * @return bool
49
     */
50
    protected function hasPermission($permission)
51
    {
52
        $userService = $this->get('user');
53
        $user = $userService->getCurrentUser();
54
        if (!$user->hasPermission($permission)) {
55
            throw new AccessDeniedException();
56
        }
57
    }
58
}
59