Completed
Push — 2.x-dev-kit ( 8d77e1 )
by
unknown
28:22 queued 25:50
created

MessageAdminController::batchActionPublish()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata project.
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\NotificationBundle\Controller;
13
14
use Sonata\AdminBundle\Controller\CRUDController;
15
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
16
use Sonata\NotificationBundle\Backend\BackendInterface;
17
use Sonata\NotificationBundle\Model\MessageManagerInterface;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
20
21
class MessageAdminController extends CRUDController
22
{
23
    /**
24
     * @param ProxyQueryInterface $query
25
     *
26
     * @throws AccessDeniedException
27
     *
28
     * @return RedirectResponse
29
     */
30
    public function batchActionPublish(ProxyQueryInterface $query)
31
    {
32
        if (false === $this->admin->isGranted('EDIT')) {
33
            throw new AccessDeniedException();
34
        }
35
36
        foreach ($query->execute() as $message) {
37
            $message = $this->getMessageManager()->restart($message);
38
39
            $this->getBackend()->publish($message);
40
        }
41
42
        return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
43
    }
44
45
    /**
46
     * @param ProxyQueryInterface $query
47
     *
48
     * @throws AccessDeniedException
49
     *
50
     * @return RedirectResponse
51
     */
52
    public function batchActionCancelled(ProxyQueryInterface $query)
53
    {
54
        if (false === $this->admin->isGranted('EDIT')) {
55
            throw new AccessDeniedException();
56
        }
57
58
        foreach ($query->execute() as $message) {
59
            $this->getMessageManager()->cancel($message);
60
        }
61
62
        return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
63
    }
64
65
    /**
66
     * @return MessageManagerInterface
67
     */
68
    protected function getMessageManager()
69
    {
70
        return $this->get('sonata.notification.manager.message');
71
    }
72
73
    /**
74
     * @return BackendInterface
75
     */
76
    protected function getBackend()
77
    {
78
        return $this->get('sonata.notification.backend');
79
    }
80
}
81