CommentAdminController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 40
rs 10
c 0
b 0
f 0

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