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

UserProfileDeleteController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
}