1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NotificationBundle\Controller\Api; |
15
|
|
|
|
16
|
|
|
use FOS\RestBundle\Controller\Annotations as Rest; |
17
|
|
|
use FOS\RestBundle\Request\ParamFetcherInterface; |
18
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
19
|
|
|
use Sonata\DatagridBundle\Pager\PagerInterface; |
20
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
21
|
|
|
use Sonata\NotificationBundle\Model\MessageManagerInterface; |
22
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Hugo Briand <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class MessageController |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var MessageManagerInterface |
32
|
|
|
*/ |
33
|
|
|
protected $messageManager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var FormFactoryInterface |
37
|
|
|
*/ |
38
|
|
|
protected $formFactory; |
39
|
|
|
|
40
|
|
|
public function __construct(MessageManagerInterface $messageManager, FormFactoryInterface $formFactory) |
41
|
|
|
{ |
42
|
|
|
$this->messageManager = $messageManager; |
43
|
|
|
$this->formFactory = $formFactory; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Retrieves the list of messages (paginated). |
48
|
|
|
* |
49
|
|
|
* @ApiDoc( |
50
|
|
|
* resource=true, |
51
|
|
|
* output={"class"="Sonata\DatagridBundle\Pager\PagerInterface", "groups"={"sonata_api_read"}} |
52
|
|
|
* ) |
53
|
|
|
* |
54
|
|
|
* @Rest\QueryParam(name="page", requirements="\d+", default="1", description="Page for message list pagination") |
55
|
|
|
* @Rest\QueryParam(name="count", requirements="\d+", default="10", description="Number of messages by page") |
56
|
|
|
* @Rest\QueryParam(name="type", nullable=true, description="Message type filter") |
57
|
|
|
* @Rest\QueryParam(name="state", requirements="\d+", strict=true, nullable=true, description="Message status filter") |
58
|
|
|
* @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)") |
59
|
|
|
* |
60
|
|
|
* @Rest\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
61
|
|
|
* |
62
|
|
|
* @return PagerInterface |
63
|
|
|
*/ |
64
|
|
|
public function getMessagesAction(ParamFetcherInterface $paramFetcher) |
65
|
|
|
{ |
66
|
|
|
$supportedCriteria = [ |
67
|
|
|
'state' => '', |
68
|
|
|
'type' => '', |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
$page = $paramFetcher->get('page'); |
72
|
|
|
$limit = $paramFetcher->get('count'); |
73
|
|
|
$sort = $paramFetcher->get('orderBy'); |
74
|
|
|
$criteria = array_intersect_key($paramFetcher->all(), $supportedCriteria); |
75
|
|
|
|
76
|
|
|
foreach ($criteria as $key => $value) { |
77
|
|
|
if (null === $value) { |
78
|
|
|
unset($criteria[$key]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (!$sort) { |
83
|
|
|
$sort = []; |
84
|
|
|
} elseif (!\is_array($sort)) { |
85
|
|
|
$sort = [$sort => 'asc']; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->getMessageManager()->getPager($criteria, $page, $limit, $sort); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Adds a message. |
93
|
|
|
* |
94
|
|
|
* @ApiDoc( |
95
|
|
|
* input={"class"="sonata_notification_api_form_message", "name"="", "groups"={"sonata_api_write"}}, |
96
|
|
|
* output={"class"="Sonata\NotificationBundle\Model\Message", "groups"={"sonata_api_read"}}, |
97
|
|
|
* statusCodes={ |
98
|
|
|
* 200="Returned when successful", |
99
|
|
|
* 400="Returned when an error has occurred while message creation" |
100
|
|
|
* } |
101
|
|
|
* ) |
102
|
|
|
* |
103
|
|
|
* @Rest\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
104
|
|
|
* |
105
|
|
|
* @param Request $request A Symfony request |
106
|
|
|
* |
107
|
|
|
* @return MessageInterface |
108
|
|
|
*/ |
109
|
|
|
public function postMessageAction(Request $request) |
110
|
|
|
{ |
111
|
|
|
$message = null; |
112
|
|
|
|
113
|
|
|
$form = $this->formFactory->createNamed(null, 'sonata_notification_api_form_message', $message, [ |
114
|
|
|
'csrf_protection' => false, |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
$form->handleRequest($request); |
118
|
|
|
|
119
|
|
|
if ($form->isValid()) { |
120
|
|
|
$message = $form->getData(); |
121
|
|
|
$this->messageManager->save($message); |
122
|
|
|
|
123
|
|
|
return $message; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $form; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return MessageManagerInterface |
131
|
|
|
*/ |
132
|
|
|
protected function getMessageManager() |
133
|
|
|
{ |
134
|
|
|
return $this->messageManager; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.