forked from FriendsOfSymfony/FOSMessageBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractMessageFormHandler.php
86 lines (73 loc) · 2.3 KB
/
AbstractMessageFormHandler.php
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
<?php
namespace Ornicar\MessageBundle\FormHandler;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Ornicar\MessageBundle\Composer\ComposerInterface;
use Ornicar\MessageBundle\FormModel\AbstractMessage;
use Ornicar\MessageBundle\Security\ParticipantProviderInterface;
use Ornicar\MessageBundle\Model\ParticipantInterface;
use Ornicar\MessageBundle\Sender\SenderInterface;
/**
* Handles messages forms, from binding request to sending the message
*
* @author Thibault Duplessis <[email protected]>
*/
abstract class AbstractMessageFormHandler
{
protected $request;
protected $composer;
protected $sender;
protected $participantProvider;
public function __construct(Request $request, ComposerInterface $composer, SenderInterface $sender, ParticipantProviderInterface $participantProvider)
{
$this->request = $request;
$this->composer = $composer;
$this->sender = $sender;
$this->participantProvider = $participantProvider;
}
/**
* Processes the form with the request
*
* @param Form $form
* @return Message|false the sent message if the form is bound and valid, false otherwise
*/
public function process(Form $form)
{
if ('POST' !== $this->request->getMethod()) {
return false;
}
$form->bindRequest($this->request);
if ($form->isValid()) {
return $this->processValidForm($form);
}
return false;
}
/**
* Processes the valid form, sends the message
*
* @param 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();
}
}