Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
Upgrade FosRest bundle to 2.1 (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
KDederichs authored and OskarStark committed Dec 22, 2016
1 parent 7327545 commit 1484b93
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
36 changes: 31 additions & 5 deletions Controller/Api/MessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sonata\NotificationBundle\Controller\Api;

use FOS\RestBundle\Context\Context;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\Route;
use FOS\RestBundle\Controller\Annotations\View;
Expand Down Expand Up @@ -64,7 +65,6 @@ public function __construct(MessageManagerInterface $messageManager, FormFactory
*
* @QueryParam(name="page", requirements="\d+", default="1", description="Page for message list pagination")
* @QueryParam(name="count", requirements="\d+", default="10", description="Number of messages by page")
* @QueryParam(name="orderBy", array=true, requirements="ASC|DESC", nullable=true, strict=true, description="Order by array (key is field, value is direction)")
* @QueryParam(name="type", nullable=true, description="Message type filter")
* @QueryParam(name="state", requirements="\d+", strict=true, nullable=true, description="Message status filter")
*
Expand All @@ -76,6 +76,20 @@ public function __construct(MessageManagerInterface $messageManager, FormFactory
*/
public function getMessagesAction(ParamFetcherInterface $paramFetcher)
{
$orderByQueryParam = new QueryParam();
$orderByQueryParam->name = 'orderBy';
$orderByQueryParam->requirements = 'ASC|DESC';
$orderByQueryParam->nullable = true;
$orderByQueryParam->strict = true;
$orderByQueryParam->description = 'Query groups order by clause (key is field, value is direction)';
if (property_exists($orderByQueryParam, 'map')) {
$orderByQueryParam->map = true;
} else {
$orderByQueryParam->array = true;
}

$paramFetcher->addParam($orderByQueryParam);

$supportedCriteria = array(
'state' => '',
'type' => '',
Expand Down Expand Up @@ -135,10 +149,22 @@ public function postMessageAction(Request $request)
$this->messageManager->save($message);

$view = FOSRestView::create($message);
$serializationContext = SerializationContext::create();
$serializationContext->setGroups(array('sonata_api_read'));
$serializationContext->enableMaxDepthChecks();
$view->setSerializationContext($serializationContext);

if (class_exists('FOS\RestBundle\Context\Context')) {
$serializationContext = new Context();
if (method_exists($serializationContext, 'enableMaxDepth')) {
$serializationContext->enableMaxDepth();
} else {
$serializationContext->setMaxDepth(10);
}
$serializationContext->setGroups(array('sonata_api_read'));
$view->setContext($serializationContext);
} else {
$serializationContext = SerializationContext::create();
$serializationContext->enableMaxDepthChecks();
$serializationContext->setGroups(array('sonata_api_read'));
$view->setSerializationContext($serializationContext);
}

return $view;
}
Expand Down
4 changes: 3 additions & 1 deletion Tests/Controller/Api/MessageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public function testGetMessagesAction()
$messageManager = $this->getMock('Sonata\NotificationBundle\Model\MessageManagerInterface');
$messageManager->expects($this->once())->method('getPager')->will($this->returnValue(array()));

$paramFetcher = $this->getMock('FOS\RestBundle\Request\ParamFetcherInterface');
$paramFetcher = $this->getMockBuilder('FOS\RestBundle\Request\ParamFetcher')
->disableOriginalConstructor()
->getMock();
$paramFetcher->expects($this->exactly(3))->method('get');
$paramFetcher->expects($this->once())->method('all')->will($this->returnValue(array()));

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"liip/monitor-bundle": "^2.0",
"symfony/phpunit-bridge": "^2.7 || ^3.0",
"swiftmailer/swiftmailer": "^4.3 || ^5.0",
"friendsofsymfony/rest-bundle": "^1.1",
"friendsofsymfony/rest-bundle": "^1.5 || ^2.1",
"jms/serializer-bundle": "^0.13 || ^1.0",
"nelmio/api-doc-bundle": "^2.4",
"sllh/php-cs-fixer-styleci-bridge": "^2.0"
Expand Down

0 comments on commit 1484b93

Please sign in to comment.