-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathAbstractMessageFormHandler.php
More file actions
126 lines (108 loc) · 3.9 KB
/
AbstractMessageFormHandler.php
File metadata and controls
126 lines (108 loc) · 3.9 KB
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
<?php
namespace FOS\MessageBundle\FormHandler;
use FOS\MessageBundle\Composer\ComposerInterface;
use FOS\MessageBundle\FormModel\AbstractMessage;
use FOS\MessageBundle\Model\MessageInterface;
use FOS\MessageBundle\Model\ParticipantInterface;
use FOS\MessageBundle\Security\ParticipantProviderInterface;
use FOS\MessageBundle\Sender\SenderInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Handles messages forms, from binding request to sending the message.
*
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
*/
abstract class AbstractMessageFormHandler
{
protected $request;
protected $composer;
protected $sender;
protected $participantProvider;
/**
* @param Request|RequestStack $request
* @param ComposerInterface $composer
* @param SenderInterface $sender
* @param ParticipantProviderInterface $participantProvider
*/
public function __construct($request, ComposerInterface $composer, SenderInterface $sender, ParticipantProviderInterface $participantProvider)
{
if ($request instanceof Request) {
@trigger_error(sprintf('Using an instance of "%s" as first parameter of "%s" is deprecated since version 1.3 and won\'t be supported in 2.0. Use an instance of "Symfony\Component\HttpFoundation\RequestStack" instead.', get_class($request), __METHOD__), E_USER_DEPRECATED);
} elseif (!$request instanceof RequestStack) {
throw new \InvalidArgumentException(sprintf('AbstractMessageFormHandler expected a Request or RequestStack, %s given', is_object($request) ? get_class($request) : gettype($request)));
}
$this->request = $request;
$this->composer = $composer;
$this->sender = $sender;
$this->participantProvider = $participantProvider;
}
/**
* Processes the form with the request.
*
* @param Form $form
*
* @return MessageInterface|false the sent message if the form is bound and valid, false otherwise
*/
public function process(Form $form)
{
$request = $this->getCurrentRequest();
if ('POST' !== $request->getMethod()) {
return false;
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
return $this->processValidForm($form);
}
return false;
}
/**
* Processes the valid form, sends the message.
*
* @param Form $form
*
* @return MessageInterface the sent message
*/
public function processValidForm(Form $form)
{
$message = $this->composeMessage($form->getData());
$this->sender->send($message);
return $message;
}
/**
* Composes a message from the form data.
*
* @param AbstractMessage $message
*
* @return MessageInterface the composed message ready to be sent
*/
abstract protected function composeMessage(AbstractMessage $message);
/**
* Gets the current authenticated user.
*
* @return ParticipantInterface
*/
protected function getAuthenticatedParticipant()
{
return $this->participantProvider->getAuthenticatedParticipant();
}
/**
* BC layer to retrieve the current request directly or from a stack.
*
* @return Request
*/
private function getCurrentRequest()
{
if (!$this->request) {
throw new \RuntimeException('Current request was not provided to the form handler.');
}
if ($this->request instanceof Request) {
return $this->request;
}
if (!$this->request->getCurrentRequest()) {
throw new \RuntimeException('Request stack provided to the form handler did not contains a current request.');
}
return $this->request->getCurrentRequest();
}
}