diff --git a/Controller/Api/MessageController.php b/Controller/Api/MessageController.php index 40bf9d39..420bcc52 100644 --- a/Controller/Api/MessageController.php +++ b/Controller/Api/MessageController.php @@ -13,11 +13,17 @@ use FOS\RestBundle\Controller\Annotations\QueryParam; use FOS\RestBundle\Controller\Annotations\View; +use FOS\RestBundle\Controller\Annotations\Route; use FOS\RestBundle\Request\ParamFetcherInterface; +use FOS\RestBundle\View\View as FOSRestView; +use JMS\Serializer\SerializationContext; use Nelmio\ApiDocBundle\Annotation\ApiDoc; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormFactoryInterface; -use Sonata\NotificationBundle\Model\Message; +use Sonata\NotificationBundle\Model\MessageInterface; use Sonata\NotificationBundle\Model\MessageManagerInterface; /** @@ -34,14 +40,21 @@ class MessageController */ protected $messageManager; + /** + * @var FormFactoryInterface + */ + protected $formFactory; + /** * Constructor * * @param MessageManagerInterface $messageManager + * @param FormFactoryInterface $formFactory */ - public function __construct(MessageManagerInterface $messageManager) + public function __construct(MessageManagerInterface $messageManager, FormFactoryInterface $formFactory) { $this->messageManager = $messageManager; + $this->formFactory = $formFactory; } /** @@ -92,6 +105,51 @@ public function getMessagesAction(ParamFetcherInterface $paramFetcher) 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" + * } + * ) + * + * @Route(requirements={"_format"="json|xml"}) + * + * @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, array( + 'csrf_protection' => false, + )); + + $form->bind($request); + + if ($form->isValid()) { + $message = $form->getData(); + + $this->messageManager->save($message); + + $view = FOSRestView::create($message); + $serializationContext = SerializationContext::create(); + $serializationContext->setGroups(array('sonata_api_read')); + $serializationContext->enableMaxDepthChecks(); + $view->setSerializationContext($serializationContext); + + return $view; + } + + return $form; + } + /** * @return MessageManagerInterface */ diff --git a/DependencyInjection/SonataNotificationExtension.php b/DependencyInjection/SonataNotificationExtension.php index 999ce3f8..ef9a704e 100644 --- a/DependencyInjection/SonataNotificationExtension.php +++ b/DependencyInjection/SonataNotificationExtension.php @@ -55,6 +55,7 @@ public function load(array $configs, ContainerBuilder $container) if (isset($bundles['FOSRestBundle']) && isset($bundles['NelmioApiDocBundle'])) { $loader->load('api_controllers.xml'); + $loader->load('api_form.xml'); } if ($config['admin']['enabled'] && isset($bundles['SonataDoctrineORMAdminBundle'])) { // for now, only support for ORM diff --git a/Resources/config/api_controllers.xml b/Resources/config/api_controllers.xml index 35fb87da..ea337265 100644 --- a/Resources/config/api_controllers.xml +++ b/Resources/config/api_controllers.xml @@ -7,6 +7,7 @@ + diff --git a/Resources/config/api_form.xml b/Resources/config/api_form.xml new file mode 100644 index 00000000..9e6b5cde --- /dev/null +++ b/Resources/config/api_form.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + sonata_notification_api_form_message + %sonata.notification.admin.message.entity% + sonata_api_write + + + diff --git a/Resources/config/serializer/Model.Message.xml b/Resources/config/serializer/Model.Message.xml index 54cf4efb..de3a8ced 100644 --- a/Resources/config/serializer/Model.Message.xml +++ b/Resources/config/serializer/Model.Message.xml @@ -3,7 +3,7 @@ - + diff --git a/Resources/config/validation.xml b/Resources/config/validation.xml new file mode 100644 index 00000000..2d36447e --- /dev/null +++ b/Resources/config/validation.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + diff --git a/Tests/Controller/Api/MessageControllerTest.php b/Tests/Controller/Api/MessageControllerTest.php index 844d35db..c3a6a5a1 100644 --- a/Tests/Controller/Api/MessageControllerTest.php +++ b/Tests/Controller/Api/MessageControllerTest.php @@ -10,7 +10,7 @@ namespace Sonata\NotificationBundle\Tests\Controller\Api; - +use Symfony\Component\HttpFoundation\Request; use Sonata\NotificationBundle\Controller\Api\MessageController; @@ -35,6 +35,45 @@ public function testGetMessagesAction() $this->assertEquals(array(), $this->createMessageController(null, $messageManager)->getMessagesAction($paramFetcher)); } + public function testPostMessageAction() + { + $message = $this->getMock('Sonata\NotificationBundle\Model\MessageInterface'); + + $messageManager = $this->getMock('Sonata\NotificationBundle\Model\MessageManagerInterface'); + $messageManager->expects($this->once())->method('save')->will($this->returnValue($message)); + + $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock(); + $form->expects($this->once())->method('bind'); + $form->expects($this->once())->method('isValid')->will($this->returnValue(true)); + $form->expects($this->once())->method('getData')->will($this->returnValue($message)); + + $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface'); + $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form)); + + $view = $this->createMessageController(null, $messageManager, $formFactory)->postMessageAction(new Request()); + + $this->assertInstanceOf('FOS\RestBundle\View\View', $view); + } + + public function testPostMessageInvalidAction() + { + $message = $this->getMock('Sonata\NotificationBundle\Model\MessageInterface'); + + $messageManager = $this->getMock('Sonata\NotificationBundle\Model\MessageManagerInterface'); + $messageManager->expects($this->never())->method('save')->will($this->returnValue($message)); + + $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock(); + $form->expects($this->once())->method('bind'); + $form->expects($this->once())->method('isValid')->will($this->returnValue(false)); + + $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface'); + $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form)); + + $view = $this->createMessageController(null, $messageManager, $formFactory)->postMessageAction(new Request()); + + $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view); + } + /** * @param $message * @param $messageManager