CmsBlockController::cmsFilters()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 17

Duplication

Lines 22
Ratio 100 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 22
loc 22
rs 8.6737
cc 5
eloc 17
nc 5
nop 3
1
<?php
2
3
namespace AdminBundle\Controller;
4
5
use AdminBundle\Form\CmsBlockType;
6
use AppBundle\Entity\CmsBlock;
7
use AppBundle\Controller\DoctrineController;
8
use DataDog\PagerBundle\Pagination;
9
use Doctrine\ORM\QueryBuilder;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
13
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * @Route("/cms")
18
 */
19
class CmsBlockController extends Controller
20
{
21
    use DoctrineController;
22
23
    /**
24
     * @Route("/")
25
     * @Method("GET")
26
     * @Template
27
     *
28
     * @param Request $request
29
     * @return \Symfony\Component\HttpFoundation\Response
30
     */
31
    public function indexAction(Request $request)
32
    {
33
        $blocks = $this->get('em')->getRepository('AppBundle:CmsBlock')->createQueryBuilder('t');
34
35
        return [
36
            'blocks' => new Pagination($blocks, $request, [
37
                'applyFilter' => [$this, 'cmsFilters'],
38
            ]),
39
        ];
40
    }
41
42
    /**
43
     * Displays a form to create a new CmsBlock entity.
44
     *
45
     * @Route("/new")
46
     * @Method({"GET", "POST"})
47
     * @Template
48
     *
49
     * @param Request $request
50
     * @return \Symfony\Component\HttpFoundation\Response
51
     */
52
    public function newAction(Request $request)
53
    {
54
        $block = new CmsBlock();
55
        $form = $this->createForm(new CmsBlockType(), $block);
56
        $form->handleRequest($request);
57
58
        if (!$form->isValid()) {
59
            return [
60
                'entity' => $block,
61
                'form' => $form->createView(),
62
            ];
63
        }
64
65
        $this->persist($block);
66
        $this->flush();
67
        $this->addFlash("success", $this->get('translator')->trans('cms_block.flash.created'));
68
69
        return $this->redirectToRoute('admin_cmsblock_index');
70
    }
71
72
73
    /**
74
     * Displays a form to edit an existing CmsBlock entity.
75
     *
76
     * @Route("/{id}/edit")
77
     * @Method({"GET", "POST"})
78
     * @Template
79
     *
80
     * @param CmsBlock $block
81
     * @param Request $request
82
     * @return \Symfony\Component\HttpFoundation\Response
83
     */
84 View Code Duplication
    public function editAction(CmsBlock $block, Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        $form = $this->createForm(new CmsBlockType(), $block);
87
        $form->handleRequest($request);
88
89
        if (!$form->isValid()) {
90
            return [
91
                'form' => $form->createView(),
92
                'entity' => $block,
93
            ];
94
        }
95
        $this->persist($block);
96
        $this->flush();
97
        // refresh cache
98
        $this->get('cache.default')->delete('cms_block.' . $block->getAlias());
99
        $this->addFlash("success", $this->get('translator')->trans('cms_block.flash.updated'));
100
101
        return $this->redirectToRoute('admin_cmsblock_index');
102
    }
103
104
    /**
105
     * Deletes a CmsBlock entity.
106
     *
107
     * @Route("/{id}/delete")
108
     * @Method("GET")
109
     *
110
     * @param CmsBlock $block
111
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
112
     */
113
    public function deleteAction(CmsBlock $block)
114
    {
115
        $this->remove($block);
116
        $this->flush();
117
        $this->addFlash("danger", $this->get('translator')->trans('cms_block.flash.removed'));
118
119
        return $this->redirectToRoute('admin_cmsblock_index');
120
    }
121
122
    /**
123
     * Our filter handler function, which allows us to
124
     * modify the query builder specifically for our filter option
125
     * @param QueryBuilder $qb
126
     * @param string $key
127
     * @param string $val
128
     */
129 View Code Duplication
    public function cmsFilters(QueryBuilder $qb, $key, $val)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        if (empty($val)) {
132
            return;
133
        }
134
135
        switch ($key) {
136
        case 't.alias':
137
            $qb->andWhere($qb->expr()->like('t.alias', ':alias'));
138
            $qb->setParameter('alias', "%$val%");
139
            break;
140
        case 't.name':
141
            $qb->andWhere($qb->expr()->like('t.name', ':name'));
142
            $qb->setParameter('name', "%$val%");
143
            break;
144
        case 't.updatedAt':
145
            $date = date("Y-m-d", strtotime($val));
146
            $qb->andWhere($qb->expr()->gt('t.updatedAt', "'$date 00:00:00'"));
147
            $qb->andWhere($qb->expr()->lt('t.updatedAt', "'$date 23:59:59'"));
148
            break;
149
        }
150
    }
151
}
152