ConversationFormController::show()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 15
rs 10
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.apps')) {
91
            foreach ($this->params->get('pwc.apps') as $app) {
92
                foreach ($app['hosts'] as $host) {
93
                    $this->possibleOrigins[] = 'https://'.$host;
94
                }
95
            }
96
        }
97
98
        return $this->possibleOrigins;
99
    }
100
101
    protected function initResponse($request)
102
    {
103
        $response = new Response();
104
105
        if (!in_array($request->headers->get('origin'), $this->getPossibleOrigins($request))) {
106
            return;
107
        }
108
109
        $response->headers->set('Access-Control-Allow-Credentials', 'true');
110
        $response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS');
111
        $response->headers->set('Access-Control-Allow-Headers', 'Origin, Content-Type, X-Auth-Token');
112
        $response->headers->set('Access-Control-Allow-Origin', $request->headers->get('origin'));
113
114
        return $response;
115
    }
116
117
    public function show(string $type, Request $request)
118
    {
119
        $response = $this->initResponse($request);
120
        if (null === $response) {
121
            throw new Exception('origin not verified');
122
        }
123
124
        $form = $this->getFormManager($type, $request)->getCurrentStep()->getForm();
125
        $form->handleRequest($request);
126
127
        if ($form->isSubmitted()) {
128
            return $response->setContent($this->getFormManager($type, $request)->validCurrentStep($form));
129
        }
130
131
        return $response->setContent($this->getFormManager($type, $request)->showForm($form));
132
    }
133
}
134