AdminAdvertController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A publish() 0 9 1
A index() 0 6 1
A unpublish() 0 9 1
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\Advert;
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\Routing\Annotation\Route;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
9
10
/**
11
 * @Security("has_role('ROLE_ADMIN')")
12
 */
13
class AdminAdvertController extends AbstractController
14
{
15
    /**
16
     * @Route("/admin/advert", name="admin_adverts")
17
     */
18
    public function index()
19
    {
20
        $em = $this->getDoctrine()->getManager();
21
        $listAdverts = $em->getRepository(Advert::class)->findAll();
22
        return $this->render('admin_advert/index.html.twig', [
23
            'adverts' => $listAdverts,
24
        ]);
25
    }
26
27
    /**
28
     * @Route("/admin/advert/unpublish/{id}", name="unpublish_advert")
29
     */
30
    public function unpublish($id)
31
    {
32
        $em = $this->getDoctrine()->getManager();
33
        $advert = $em->getRepository(Advert::class)->find($id);
34
        $advert->setPublished(false);
35
        $em->flush();
36
37
        $this->addFlash('success', 'Cette annonce a été dépubliée');
38
        return $this->redirectToRoute('admin_adverts');
39
    }
40
41
    /**
42
     * @Route("/admin/advert/publish/{id}", name="publish_advert")
43
     */
44
    public function publish($id)
45
    {
46
        $em = $this->getDoctrine()->getManager();
47
        $advert = $em->getRepository(Advert::class)->find($id);
48
        $advert->setPublished(true);
49
        $em->flush();
50
51
        $this->addFlash('success', 'Cette annonce a été validée');
52
        return $this->redirectToRoute('admin_adverts');
53
    }
54
}
55