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\NotificationBundle\Controller\Api; |
13
|
|
|
|
14
|
|
|
use FOS\RestBundle\Controller\Annotations\QueryParam; |
15
|
|
|
use FOS\RestBundle\Controller\Annotations\Route; |
16
|
|
|
use FOS\RestBundle\Controller\Annotations\View; |
17
|
|
|
use FOS\RestBundle\Request\ParamFetcherInterface; |
18
|
|
|
use FOS\RestBundle\View\View as FOSRestView; |
19
|
|
|
use JMS\Serializer\SerializationContext; |
20
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
21
|
|
|
use Sonata\DatagridBundle\Pager\PagerInterface; |
22
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
23
|
|
|
use Sonata\NotificationBundle\Model\MessageManagerInterface; |
24
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
25
|
|
|
use Symfony\Component\HttpFoundation\Request; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class MessageController. |
29
|
|
|
* |
30
|
|
|
* |
31
|
|
|
* @author Hugo Briand <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class MessageController |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var MessageManagerInterface |
37
|
|
|
*/ |
38
|
|
|
protected $messageManager; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var FormFactoryInterface |
42
|
|
|
*/ |
43
|
|
|
protected $formFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor. |
47
|
|
|
* |
48
|
|
|
* @param MessageManagerInterface $messageManager |
49
|
|
|
* @param FormFactoryInterface $formFactory |
50
|
|
|
*/ |
51
|
|
|
public function __construct(MessageManagerInterface $messageManager, FormFactoryInterface $formFactory) |
52
|
|
|
{ |
53
|
|
|
$this->messageManager = $messageManager; |
54
|
|
|
$this->formFactory = $formFactory; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Retrieves the list of messages (paginated). |
59
|
|
|
* |
60
|
|
|
* @ApiDoc( |
61
|
|
|
* resource=true, |
62
|
|
|
* output={"class"="Sonata\DatagridBundle\Pager\PagerInterface", "groups"="sonata_api_read"} |
63
|
|
|
* ) |
64
|
|
|
* |
65
|
|
|
* @QueryParam(name="page", requirements="\d+", default="1", description="Page for message list pagination") |
66
|
|
|
* @QueryParam(name="count", requirements="\d+", default="10", description="Number of messages by page") |
67
|
|
|
* @QueryParam(name="orderBy", array=true, requirements="ASC|DESC", nullable=true, strict=true, description="Order by array (key is field, value is direction)") |
68
|
|
|
* @QueryParam(name="type", nullable=true, description="Message type filter") |
69
|
|
|
* @QueryParam(name="state", requirements="\d+", strict=true, nullable=true, description="Message status filter") |
70
|
|
|
* |
71
|
|
|
* @View(serializerGroups="sonata_api_read", serializerEnableMaxDepthChecks=true) |
72
|
|
|
* |
73
|
|
|
* @param ParamFetcherInterface $paramFetcher |
74
|
|
|
* |
75
|
|
|
* @return PagerInterface |
76
|
|
|
*/ |
77
|
|
|
public function getMessagesAction(ParamFetcherInterface $paramFetcher) |
78
|
|
|
{ |
79
|
|
|
$supportedCriteria = array( |
80
|
|
|
'state' => '', |
81
|
|
|
'type' => '', |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$page = $paramFetcher->get('page'); |
85
|
|
|
$limit = $paramFetcher->get('count'); |
86
|
|
|
$sort = $paramFetcher->get('orderBy'); |
87
|
|
|
$criteria = array_intersect_key($paramFetcher->all(), $supportedCriteria); |
88
|
|
|
|
89
|
|
|
foreach ($criteria as $key => $value) { |
90
|
|
|
if (null === $value) { |
91
|
|
|
unset($criteria[$key]); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!$sort) { |
96
|
|
|
$sort = array(); |
97
|
|
|
} elseif (!is_array($sort)) { |
98
|
|
|
$sort = array($sort => 'asc'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->getMessageManager()->getPager($criteria, $page, $limit, $sort); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Adds a message. |
106
|
|
|
* |
107
|
|
|
* @ApiDoc( |
108
|
|
|
* input={"class"="sonata_notification_api_form_message", "name"="", "groups"={"sonata_api_write"}}, |
109
|
|
|
* output={"class"="Sonata\NotificationBundle\Model\Message", "groups"={"sonata_api_read"}}, |
110
|
|
|
* statusCodes={ |
111
|
|
|
* 200="Returned when successful", |
112
|
|
|
* 400="Returned when an error has occurred while message creation" |
113
|
|
|
* } |
114
|
|
|
* ) |
115
|
|
|
* |
116
|
|
|
* @Route(requirements={"_format"="json|xml"}) |
117
|
|
|
* |
118
|
|
|
* @param Request $request A Symfony request |
119
|
|
|
* |
120
|
|
|
* @return MessageInterface |
121
|
|
|
*/ |
122
|
|
|
public function postMessageAction(Request $request) |
123
|
|
|
{ |
124
|
|
|
$message = null; |
125
|
|
|
|
126
|
|
|
$form = $this->formFactory->createNamed(null, 'sonata_notification_api_form_message', $message, array( |
127
|
|
|
'csrf_protection' => false, |
128
|
|
|
)); |
129
|
|
|
|
130
|
|
|
$form->handleRequest($request); |
131
|
|
|
|
132
|
|
|
if ($form->isValid()) { |
133
|
|
|
$message = $form->getData(); |
134
|
|
|
|
135
|
|
|
$this->messageManager->save($message); |
136
|
|
|
|
137
|
|
|
$view = FOSRestView::create($message); |
138
|
|
|
$serializationContext = SerializationContext::create(); |
139
|
|
|
$serializationContext->setGroups(array('sonata_api_read')); |
140
|
|
|
$serializationContext->enableMaxDepthChecks(); |
141
|
|
|
$view->setSerializationContext($serializationContext); |
|
|
|
|
142
|
|
|
|
143
|
|
|
return $view; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $form; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return MessageManagerInterface |
151
|
|
|
*/ |
152
|
|
|
protected function getMessageManager() |
153
|
|
|
{ |
154
|
|
|
return $this->messageManager; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.