|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\ConversationBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
11
|
|
|
|
|
12
|
|
|
class ConversationFormController extends AbstractController |
|
13
|
|
|
{ |
|
14
|
|
|
private $translator; |
|
15
|
|
|
|
|
16
|
|
|
protected $form; |
|
17
|
|
|
|
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
protected $possibleOrigins = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var ParameterBagInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $params; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
TranslatorInterface $translator, |
|
28
|
|
|
ParameterBagInterface $params |
|
29
|
|
|
) { |
|
30
|
|
|
$this->translator = $translator; |
|
31
|
|
|
$this->params = $params; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function getFormManagerClass($type) |
|
35
|
|
|
{ |
|
36
|
|
|
$param = 'pwc.conversation.form'.($this->params->has('pwc.conversation.form.'.$type) ? '.'.$type : '_'.$type); |
|
37
|
|
|
|
|
38
|
|
|
if (!$this->params->has($param)) { |
|
39
|
|
|
throw new \Exception('`'.$type.'` does\'nt exist (not configured).'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$class = $this->params->get($param); |
|
43
|
|
|
if (!class_exists($class)) { |
|
44
|
|
|
throw new \Exception('`'.$type.'` does\'nt exist.'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $class; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Return current form manager depending on `type` (request). |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function getFormManager(string $type, Request $request) |
|
54
|
|
|
{ |
|
55
|
|
|
if (null !== $this->form) { |
|
56
|
|
|
return $this->form; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$class = $this->getFormManagerClass($type); |
|
60
|
|
|
|
|
61
|
|
|
return $this->form = new $class( |
|
62
|
|
|
$this->params->get('pwc.conversation.entity_message'), |
|
63
|
|
|
$request, |
|
64
|
|
|
$this->get('doctrine'), |
|
65
|
|
|
$this->get('security.token_storage'), |
|
66
|
|
|
$this->get('form.factory'), |
|
67
|
|
|
$this->get('twig'), |
|
68
|
|
|
$this->get('router'), |
|
69
|
|
|
$this->get('translator') |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function getPossibleOrigins(Request $request): array |
|
74
|
|
|
{ |
|
75
|
|
|
if (!empty($this->possibleOrigins)) { |
|
76
|
|
|
return $this->possibleOrigins; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($this->params->has('pwc.conversation.possible_origins')) { |
|
80
|
|
|
$this->possibleOrigins = explode(' ', $this->params->get('pwc.conversation.possible_origins')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->possibleOrigins[] = 'https://'.$request->getHost(); |
|
84
|
|
|
$this->possibleOrigins[] = 'http://'.$request->getHost(); |
|
85
|
|
|
// just for dev |
|
86
|
|
|
$this->possibleOrigins[] = 'http://'.$request->getHost().':8000'; |
|
87
|
|
|
$this->possibleOrigins[] = 'http://'.$request->getHost().':8001'; |
|
88
|
|
|
$this->possibleOrigins[] = 'http://'.$request->getHost().':8002'; |
|
89
|
|
|
|
|
90
|
|
|
if ($this->params->has('pwc.static.domain')) { |
|
91
|
|
|
$this->possibleOrigins[] = 'https://'.$this->params->get('pwc.static.domain'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this->possibleOrigins; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function initResponse($request) |
|
98
|
|
|
{ |
|
99
|
|
|
$response = new Response(); |
|
100
|
|
|
|
|
101
|
|
|
if (!in_array($request->headers->get('origin'), $this->getPossibleOrigins($request))) { |
|
102
|
|
|
return; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$response->headers->set('Access-Control-Allow-Credentials', 'true'); |
|
106
|
|
|
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS'); |
|
107
|
|
|
$response->headers->set('Access-Control-Allow-Headers', 'Origin, Content-Type, X-Auth-Token'); |
|
108
|
|
|
$response->headers->set('Access-Control-Allow-Origin', $request->headers->get('origin')); |
|
109
|
|
|
|
|
110
|
|
|
return $response; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function show(string $type, Request $request) |
|
114
|
|
|
{ |
|
115
|
|
|
$response = $this->initResponse($request); |
|
116
|
|
|
if (null === $response) { |
|
117
|
|
|
throw new Exception('origin not verified'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$form = $this->getFormManager($type, $request)->getCurrentStep()->getForm(); |
|
121
|
|
|
$form->handleRequest($request); |
|
122
|
|
|
|
|
123
|
|
|
if ($form->isSubmitted()) { |
|
124
|
|
|
return $response->setContent($this->getFormManager($type, $request)->validCurrentStep($form)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $response->setContent($this->getFormManager($type, $request)->showForm($form)); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|