ReportsRenderServerFactory.php
4.25 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;
require_once 'lib/Portabilis/Report/ReportFactory.php';
class Portabilis_Report_ReportsRenderServerFactory extends Portabilis_Report_ReportFactory
{
/**
* URL onde se encontra o serviço de renderização.
*
* @var string
*/
private $url;
/**
* Caminho onde estão os arquivos .jasper.
*
* @var string
*/
private $sourcePath;
/**
* Token para autorização do serviço.
*
* @var string
*/
private $token;
/**
* @var string
*/
private $logo;
/**
* @var array
*/
private $connection;
/**
* @var string
*/
private $timezone;
/**
* Loga erros da requisição ao servidor de relatórios.
*
* @param array $payload
* @param string $response
*
* @return void
*/
protected function log($payload, $response)
{
$log = json_encode([
'url' => $this->url,
'payload' => $payload,
'response' => $response,
]);
Log::error($log);
}
/**
* @inheritdoc
*/
public function setSettings($config)
{
$this->url = $config->report->remote_factory->url;
$this->sourcePath = $config->report->source_path;
$this->token = $config->report->remote_factory->token;
$this->logo = $config->report->logo_file_name;
$this->timezone = $config->app->locale->timezone;
$this->connection = [
'host' => $config->app->database->hostname,
'port' => $config->app->database->port,
'database' => $config->app->database->dbname,
'username' => $config->app->database->username,
'password' => $config->app->database->password,
];
}
/**
* Renderiza o relatório.
*
* @param Portabilis_Report_ReportCore $report
* @param array $options
*
* @throws GuzzleException
* @throws Exception
*
* @return string
*/
public function dumps($report, $options = [])
{
$options = self::mergeOptions($options, [
'add_logo_name_arg' => true,
'encoding' => 'uncoded'
]);
if ($options['add_logo_name_arg'] and !$this->logo) {
throw new Exception('The option \'add_logo_name_arg\' is true, but no logo_name defined in configurations!');
} elseif ($options['add_logo_name_arg']) {
$report->addArg('logo', $this->logo);
}
$client = new Client([
'http_errors' => false
]);
$templateName = $report->templateName();
$params = $report->args;
$params['timezone'] = $this->timezone;
if ($report->useJson()) {
$params['datasource'] = 'json';
$this->url = str_replace('/deprecated', '', $this->url);
$this->sourcePath = str_replace('/deprecated', '', $this->sourcePath);
} else {
$params['datasource'] = 'database';
$params['connection'] = 'postgresql';
$params += $this->connection;
}
$url = $this->sourcePath;
$data = $report->getJsonData();
$data = $report->modify($data);
$payload = [
'json' => [
'report' => $templateName,
'url' => $url,
'data' => $data,
'params' => $params,
],
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Token ' . $this->token,
]
];
$response = $client->request('POST', $this->url, $payload);
$json = json_decode($response->getBody()->getContents(), true);
if (is_null($json)) {
$this->log($payload, $response->getBody()->getContents());
throw new Exception('Não foi possível analisar a resposta do serviço.');
}
if ($json['success'] == false) {
$this->log($payload, $response->getBody()->getContents());
throw new Exception($json['error'] ?? $json['message']);
}
$report = base64_decode($json['data']['file']);
return $report;
}
}