This repository has been archived by the owner on Jul 28, 2022. It is now read-only.
/
MessageController.php
136 lines (117 loc) · 4.11 KB
/
MessageController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\NotificationBundle\Controller\Api;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Request\ParamFetcherInterface;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Sonata\DatagridBundle\Pager\PagerInterface;
use Sonata\NotificationBundle\Model\MessageInterface;
use Sonata\NotificationBundle\Model\MessageManagerInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* @author Hugo Briand <briand@ekino.com>
*/
class MessageController
{
/**
* @var MessageManagerInterface
*/
protected $messageManager;
/**
* @var FormFactoryInterface
*/
protected $formFactory;
public function __construct(MessageManagerInterface $messageManager, FormFactoryInterface $formFactory)
{
$this->messageManager = $messageManager;
$this->formFactory = $formFactory;
}
/**
* Retrieves the list of messages (paginated).
*
* @ApiDoc(
* resource=true,
* output={"class"="Sonata\DatagridBundle\Pager\PagerInterface", "groups"={"sonata_api_read"}}
* )
*
* @Rest\QueryParam(name="page", requirements="\d+", default="1", description="Page for message list pagination")
* @Rest\QueryParam(name="count", requirements="\d+", default="10", description="Number of messages by page")
* @Rest\QueryParam(name="type", nullable=true, description="Message type filter")
* @Rest\QueryParam(name="state", requirements="\d+", strict=true, nullable=true, description="Message status filter")
* @Rest\QueryParam(name="orderBy", map=true, requirements="ASC|DESC", nullable=true, strict=true, description="Query groups order by clause (key is field, value is direction)")
*
* @Rest\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true)
*
* @return PagerInterface
*/
public function getMessagesAction(ParamFetcherInterface $paramFetcher)
{
$supportedCriteria = [
'state' => '',
'type' => '',
];
$page = $paramFetcher->get('page');
$limit = $paramFetcher->get('count');
$sort = $paramFetcher->get('orderBy');
$criteria = array_intersect_key($paramFetcher->all(), $supportedCriteria);
foreach ($criteria as $key => $value) {
if (null === $value) {
unset($criteria[$key]);
}
}
if (!$sort) {
$sort = [];
} elseif (!\is_array($sort)) {
$sort = [$sort => 'asc'];
}
return $this->getMessageManager()->getPager($criteria, $page, $limit, $sort);
}
/**
* Adds a message.
*
* @ApiDoc(
* input={"class"="sonata_notification_api_form_message", "name"="", "groups"={"sonata_api_write"}},
* output={"class"="Sonata\NotificationBundle\Model\Message", "groups"={"sonata_api_read"}},
* statusCodes={
* 200="Returned when successful",
* 400="Returned when an error has occurred while message creation"
* }
* )
*
* @Rest\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true)
*
* @param Request $request A Symfony request
*
* @return MessageInterface
*/
public function postMessageAction(Request $request)
{
$message = null;
$form = $this->formFactory->createNamed(null, 'sonata_notification_api_form_message', $message, [
'csrf_protection' => false,
]);
$form->handleRequest($request);
if ($form->isValid()) {
$message = $form->getData();
$this->messageManager->save($message);
return $message;
}
return $form;
}
/**
* @return MessageManagerInterface
*/
protected function getMessageManager()
{
return $this->messageManager;
}
}