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

CommentAdminController::batchActionEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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