Completed
Push — 3.x-dev-kit ( 21be5a )
by
unknown
03:10
created

CommentAdminController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 5
c 3
b 2
f 0
lcom 1
cbo 6
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A batchActionEnabled() 0 4 1
A batchActionDisabled() 0 4 1
A commentChangeStatus() 0 14 3
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NewsBundle\Controller;
13
14
use Sonata\AdminBundle\Controller\CRUDController;
15
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
18
19
class CommentAdminController extends CRUDController
20
{
21
    /**
22
     * @param ProxyQueryInterface $query
23
     *
24
     * @return RedirectResponse
25
     */
26
    public function batchActionEnabled(ProxyQueryInterface $query)
27
    {
28
        return $this->commentChangeStatus($query, true);
29
    }
30
31
    /**
32
     * @param ProxyQueryInterface $query
33
     *
34
     * @return RedirectResponse
35
     */
36
    public function batchActionDisabled(ProxyQueryInterface $query)
37
    {
38
        return $this->commentChangeStatus($query, false);
39
    }
40
41
    /**
42
     * @param ProxyQueryInterface $query
43
     * @param                     $status
44
     *
45
     * @return RedirectResponse
46
     *
47
     * @throws AccessDeniedException
48
     */
49
    protected function commentChangeStatus(ProxyQueryInterface $query, $status)
50
    {
51
        if (false === $this->admin->isGranted('EDIT')) {
52
            throw new AccessDeniedException();
53
        }
54
55
        foreach ($query->execute() as $comment) {
56
            $comment->setStatus($status);
57
58
            $this->admin->getModelManager()->update($comment);
59
        }
60
61
        return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
62
    }
63
}
64