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

BaseController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 4
c 3
b 0
f 1
lcom 0
cbo 0
dl 0
loc 40
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatchEvent() 0 4 1
A dispatchNotificationEvent() 0 4 1
A hasPermission() 0 8 2
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