Passed
Push — master ( e769ca...f1e182 )
by Stone
04:38
created

UserProfileDeleteController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 10
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A deleteProfile() 0 15 2
1
<?php
2
3
namespace App\Controller\Profile;
4
5
use App\Entity\User;
6
use App\Event\User\UserDeleteAccountEvent;
7
use App\Exception\RedirectException;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Routing\Annotation\Route;
13
14
15
/**
16
 * Class UserProfileDeleteController
17
 * @package App\Controller\Profile
18
 * @IsGranted("ROLE_USER")
19
 */
20
class UserProfileDeleteController extends AbstractController
21
{
22
23
    /**
24
     * @var EventDispatcherInterface
25
     */
26
    private $dispatcher;
27
28
    public function __construct(EventDispatcherInterface $dispatcher)
29
    {
30
31
        $this->dispatcher = $dispatcher;
32
    }
33
34
    /**
35
     * @Route("/profile/delete", name="admin.delete_profile")
36
     */
37
    public function deleteProfile(Request $request)
38
    {
39
        /** @var User $user */
40
        $user = $this->getUser();
41
        $submittedToken = $request->request->get('_token');
42
        if (!$this->isCsrfTokenValid('delete-profile'.$user->getId(), $submittedToken)) {
43
            throw new RedirectException($this->generateUrl('home'), 'Bad CSRF Token');
44
        }
45
46
47
48
        $event = new UserDeleteAccountEvent($user);
49
        $this->dispatcher->dispatch(UserDeleteAccountEvent::NAME, $event);
50
51
        return $this->redirectToRoute('home');
52
53
    }
54
}