Completed
Pull Request — master (#61)
by Michal
02:17
created

ApiConsoleControl::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tomaj\NetteApi\Component;
6
7
use Nette\Application\UI\Control;
8
use Nette\Application\UI\Form;
9
use Nette\Http\IRequest;
10
use Nette\Utils\ArrayHash;
11
use Tomaj\Form\Renderer\BootstrapRenderer;
12
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface;
13
use Tomaj\NetteApi\Authorization\BearerTokenAuthorization;
14
use Tomaj\NetteApi\Authorization\NoAuthorization;
15
use Tomaj\NetteApi\EndpointInterface;
16
use Tomaj\NetteApi\Handlers\ApiHandlerInterface;
17
use Tomaj\NetteApi\Misc\ConsoleRequest;
18
19
class ApiConsoleControl extends Control
20
{
21
    private $request;
22
23
    private $endpoint;
24
25
    private $handler;
26
27
    private $authorization;
28
29
    public function __construct(IRequest $request, EndpointInterface $endpoint, ApiHandlerInterface $handler, ApiAuthorizationInterface $authorization)
30
    {
31
        $this->request = $request;
32
        $this->endpoint = $endpoint;
33
        $this->handler = $handler;
34
        $this->authorization = $authorization;
35
    }
36
37
    public function render(): void
38
    {
39
        $this->getTemplate()->setFile(__DIR__ . '/console.latte');
40
        $this->getTemplate()->add('handler', $this->handler);
41
        $this->getTemplate()->render();
42
    }
43
44
    protected function createComponentConsoleForm(): Form
45
    {
46
        $form = new Form();
47
48
        $defaults = [];
49
50
        $form->setRenderer(new BootstrapRenderer());
51
52
        $uri = $this->request->getUrl();
53
        $scheme = $uri->scheme;
54
        if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
55
            $scheme = $_SERVER['HTTP_X_FORWARDED_PROTO'];
56
        }
57
        $port = '';
58
        if ($uri->scheme === 'http' && $uri->port !== 80) {
59
            $port = ':' . $uri->port;
60
        }
61
        $url = $scheme . '://' . $uri->host . $port . '/api/' . $this->endpoint->getUrl();
62
63
        $form->addText('api_url', 'Api Url');
64
        $defaults['api_url'] = $url;
65
66
        $form->addText('api_method', 'Method');
67
        $defaults['api_method'] = $this->endpoint->getMethod();
68
69
        if ($this->authorization instanceof BearerTokenAuthorization) {
70
            $form->addText('token', 'Token')
71
                ->setHtmlAttribute('placeholder', 'Enter token');
72
        } elseif ($this->authorization instanceof NoAuthorization) {
73
            $form->addText('authorization', 'Authorization')
74
                ->setDisabled(true);
75
            $defaults['authorization'] = 'No authorization - global access';
76
        }
77
78
        $form->addCheckbox('send_session_id', 'Send session id cookie');
79
80
        $params = $this->handler->params();
81
        foreach ($params as $param) {
82
            $param->updateConsoleForm($form);
83
        }
84
85
        $form->addSubmit('send', 'Try api')
86
            ->getControlPrototype()
87
            ->setName('button')
88
            ->setHtml('<i class="fa fa-cloud-upload"></i> Try api');
89
90
        $form->setDefaults($defaults);
91
92
        $form->onSuccess[] = array($this, 'formSucceeded');
93
        return $form;
94
    }
95
96
    public function formSucceeded(Form $form, ArrayHash $values): void
97
    {
98
        $url = $values['api_url'];
99
100
        $token = false;
101
        if (isset($values['token'])) {
102
            $token = $values['token'];
103
            unset($values['token']);
104
        }
105
106
        $method = $values['api_method'];
107
        unset($values['api_method']);
108
109
        $additionalValues = [];
110
        if (isset($values['send_session_id']) && $values['send_session_id']) {
111
            $additionalValues['cookieFields'][session_name()] = session_id();
112
            session_write_close();
113
        }
114
115
        $consoleRequest = new ConsoleRequest($this->handler);
116
        $result = $consoleRequest->makeRequest($url, $method, (array) $values, $additionalValues, $token);
117
118
        $this->getTemplate()->add('response', $result);
119
120
        if ($this->getPresenter()->isAjax()) {
121
            $this->getPresenter()->redrawControl();
122
        }
123
    }
124
}
125