1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Media; |
4
|
|
|
|
5
|
|
|
use App\Entity\Image; |
6
|
|
|
use App\Event\Image\ImageDeleteEvent; |
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
|
|
|
* Class DeleteImageController |
16
|
|
|
* @package App\Controller\Image |
17
|
|
|
* @IsGranted("ROLE_USER") |
18
|
|
|
*/ |
19
|
|
|
class DeleteImageController extends AbstractController |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var EventDispatcherInterface |
24
|
|
|
*/ |
25
|
|
|
private $dispatcher; |
26
|
|
|
|
27
|
|
|
public function __construct(EventDispatcherInterface $dispatcher) |
28
|
|
|
{ |
29
|
|
|
$this->dispatcher = $dispatcher; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @Route("/image/delete/{id}", name="image.deleteFromTrick", methods={"POST"}) |
34
|
|
|
* @param Image $image |
35
|
|
|
* @param Request $request |
36
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
37
|
|
|
*/ |
38
|
|
|
public function deleteTrickImage(Image $image, Request $request) |
39
|
|
|
{ |
40
|
|
|
$submittedToken = $request->request->get('_token'); |
41
|
|
|
if (!$this->isCsrfTokenValid('delete-image' . $image->getId(), $submittedToken)) { |
42
|
|
|
throw new RedirectException($this->generateUrl('home'), 'Bad CSRF Token'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$trick = $image->getTrick(); |
46
|
|
|
if ($trick !== null){ |
47
|
|
|
$event = new ImageDeleteEvent($image, $trick); |
48
|
|
|
$this->dispatcher->dispatch(ImageDeleteEvent::NAME, $event); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->redirectToRoute('trick.edit', [ |
52
|
|
|
'id' => $trick->getId(), |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
} |