Test Setup Failed
Branch master (1b2352)
by Valery
09:42
created

PhotoController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 20
c 2
b 1
f 0
dl 0
loc 49
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A edit() 0 8 1
A delete() 0 27 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller\User;
6
7
use App\Controller\BaseController;
8
use App\Entity\Photo;
9
use App\Entity\Property;
10
use App\Service\FileUploader;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
final class PhotoController extends BaseController
17
{
18
    /**
19
     * @Route("/user/photo/{id<\d+>}/edit", name="user_photo_edit")
20
     * @IsGranted("PROPERTY_EDIT", subject="property", message="You cannot change this property.")
21
     */
22
    public function edit(Request $request, Property $property): Response
23
    {
24
        $photos = $property->getPhotos();
25
26
        return $this->render('user/photo/edit.html.twig', [
27
            'photos' => $photos,
28
            'property_id' => $property->getId(),
29
            'site' => $this->site($request),
30
        ]);
31
    }
32
33
    /**
34
     * Deletes a Photo entity.
35
     *
36
     * @Route("/user/photo/{id<\d+>}/delete", methods={"POST"}, name="user_photo_delete")
37
     */
38
    public function delete(Request $request, Photo $photo, FileUploader $fileUploader): Response
39
    {
40
        $property = $photo->getProperty();
41
42
        if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) {
43
            return $this->redirectToRoute(
44
                'user_photo_edit',
45
                ['id' => $property->getId()]
46
            );
47
        }
48
49
        // Check permissions
50
        $this->denyAccessUnlessGranted('PROPERTY_EDIT', $property);
51
52
        // Delete from db
53
        $em = $this->doctrine->getManager();
54
        $em->remove($photo);
55
        $em->flush();
56
57
        // Delete file from folder
58
        $fileUploader->remove($photo->getPhoto());
59
60
        $this->addFlash('success', 'message.deleted');
61
62
        return $this->redirectToRoute(
63
            'user_photo_edit',
64
            ['id' => $property->getId()]
65
        );
66
    }
67
}
68