1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the MilioooMessageBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Michiel boeckaert <[email protected]> |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Miliooo\Messaging\Form\FormHandler; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Form\FormInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Miliooo\Messaging\Form\FormModel\NewThreadInterface; |
16
|
|
|
use Miliooo\Messaging\Form\FormModelProcessor\NewThreadFormProcessorInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Form handler for single threads. |
20
|
|
|
* |
21
|
|
|
* @author Michiel Boeckaert <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class NewSingleThreadFormHandler extends AbstractFormHandler |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* A processor instance. |
27
|
|
|
* |
28
|
|
|
* The processor is responsible for processing the valid form model |
29
|
|
|
* |
30
|
|
|
* @var NewThreadFormProcessorInterface |
31
|
|
|
*/ |
32
|
|
|
protected $newThreadProcessor; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The request when the form was valid. |
36
|
|
|
* |
37
|
|
|
* @var Request |
38
|
|
|
*/ |
39
|
|
|
protected $request; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor. |
43
|
|
|
* |
44
|
|
|
* @param Request $request The request the form will process |
45
|
|
|
* @param NewThreadFormProcessorInterface $processor A new thread form model processor |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Request $request, NewThreadFormProcessorInterface $processor) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->request = $request; |
50
|
|
|
|
51
|
|
|
parent::__construct($request); |
52
|
|
|
$this->newThreadProcessor = $processor; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Processes the form with the request |
57
|
|
|
* |
58
|
|
|
* @param FormInterface $form A form instance |
59
|
|
|
*/ |
60
|
|
|
public function doProcess(FormInterface $form) |
61
|
|
|
{ |
62
|
|
|
//here we have the valid Form Model. This needs to be an instance of NewThreadInterface. |
63
|
|
|
|
64
|
|
|
$newThreadFormModel = $this->getFormData($form); |
65
|
|
|
|
66
|
|
|
//we update the createdAt to use the datetime when the form was successfully submitted |
67
|
|
|
$newThreadFormModel->setCreatedAt(new \DateTime('now')); |
68
|
|
|
$this->processFormModelExtra($newThreadFormModel); |
69
|
|
|
|
70
|
|
|
$this->newThreadProcessor->process($newThreadFormModel); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets the form data |
75
|
|
|
* |
76
|
|
|
* Helper function to get auto completion |
77
|
|
|
* |
78
|
|
|
* @param FormInterface $form |
79
|
|
|
* |
80
|
|
|
* @return NewThreadInterface |
81
|
|
|
* |
82
|
|
|
* @throws \InvalidArgumentException when form data is not the expected data |
83
|
|
|
*/ |
84
|
|
|
protected function getFormData(FormInterface $form) |
85
|
|
|
{ |
86
|
|
|
$data = $form->getData(); |
87
|
|
|
|
88
|
|
|
if (!$data instanceof NewThreadInterface) { |
89
|
|
|
throw new \InvalidArgumentException('Form data needs to implement NewThreadInterface'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $data; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Helper function if you need to extend this class. |
97
|
|
|
* |
98
|
|
|
* Here you have your custom form model which implements the NewThreadInterface. |
99
|
|
|
* If you need extra processing of this valid form model you can extend this class and overwrite this function. |
100
|
|
|
* |
101
|
|
|
* An example... |
102
|
|
|
* You want to store the ip address from the client. |
103
|
|
|
* $newThreadFormModel->setIpAddress = $this->request->getClientIp(); * |
104
|
|
|
* |
105
|
|
|
* @param NewThreadInterface $newThreadFormModel |
106
|
|
|
*/ |
107
|
|
|
protected function processFormModelExtra(NewThreadInterface $newThreadFormModel) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|