Code cleanup

This commit is contained in:
James Cole
2021-03-21 09:15:40 +01:00
parent da1751940e
commit 206845575c
317 changed files with 7418 additions and 7362 deletions

View File

@@ -64,6 +64,77 @@ class UpdateRequest implements UpdateRequestInterface
return $this->parseResult($updateInfo);
}
/**
* @param string $channel
*
* @return array
*/
private function contactServer(string $channel): array
{
Log::debug(sprintf('Now in contactServer(%s)', $channel));
// always fall back to current version:
$return = [
'version' => config('firefly.version'),
'date' => Carbon::today()->startOfDay(),
'level' => 'error',
'message' => (string)trans('firefly.unknown_error'),
];
$uri = config('firefly.update_endpoint');
Log::debug(sprintf('Going to call %s', $uri));
try {
$client = new Client;
$options = [
'headers' => [
'User-Agent' => sprintf('FireflyIII/%s/%s', config('firefly.version'), $channel),
],
'timeout' => 3.1415,
];
$res = $client->request('GET', $uri, $options);
} catch (GuzzleException | Exception $e) {
Log::error('Ran into Guzzle error.');
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$return['message'] = sprintf('Guzzle: %s', strip_tags($e->getMessage()));
return $return;
}
if (200 !== $res->getStatusCode()) {
Log::error(sprintf('Response status from server is %d.', $res->getStatusCode()));
Log::error((string)$res->getBody());
$return['message'] = sprintf('Error: %d', $res->getStatusCode());
return $return;
}
$body = (string)$res->getBody();
try {
$json = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException | Exception $e) {
Log::error('Body is not valid JSON');
Log::error($body);
$return['message'] = 'Invalid JSON :(';
return $return;
}
if (!isset($json['firefly_iii'][$channel])) {
Log::error(sprintf('No valid update channel "%s"', $channel));
Log::error($body);
$return['message'] = sprintf('Unknown update channel "%s" :(', $channel);
}
// parse response a bit. No message yet.
$response = $json['firefly_iii'][$channel];
$return['version'] = $response['version'];
$return['level'] = 'success';
$return['date'] = Carbon::createFromFormat('Y-m-d', $response['date'])->startOfDay();
Log::info('Response from update server', $response);
return $return;
}
/**
* @param array $information
*
@@ -149,75 +220,4 @@ class UpdateRequest implements UpdateRequestInterface
return $return;
}
/**
* @param string $channel
*
* @return array
*/
private function contactServer(string $channel): array
{
Log::debug(sprintf('Now in contactServer(%s)', $channel));
// always fall back to current version:
$return = [
'version' => config('firefly.version'),
'date' => Carbon::today()->startOfDay(),
'level' => 'error',
'message' => (string)trans('firefly.unknown_error'),
];
$uri = config('firefly.update_endpoint');
Log::debug(sprintf('Going to call %s', $uri));
try {
$client = new Client;
$options = [
'headers' => [
'User-Agent' => sprintf('FireflyIII/%s/%s', config('firefly.version'), $channel),
],
'timeout' => 3.1415
];
$res = $client->request('GET', $uri, $options);
} catch (GuzzleException|Exception $e) {
Log::error('Ran into Guzzle error.');
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$return['message'] = sprintf('Guzzle: %s', strip_tags($e->getMessage()));
return $return;
}
if (200 !== $res->getStatusCode()) {
Log::error(sprintf('Response status from server is %d.', $res->getStatusCode()));
Log::error((string)$res->getBody());
$return['message'] = sprintf('Error: %d', $res->getStatusCode());
return $return;
}
$body = (string)$res->getBody();
try {
$json = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException|Exception $e) {
Log::error('Body is not valid JSON');
Log::error($body);
$return['message'] = 'Invalid JSON :(';
return $return;
}
if (!isset($json['firefly_iii'][$channel])) {
Log::error(sprintf('No valid update channel "%s"', $channel));
Log::error($body);
$return['message'] = sprintf('Unknown update channel "%s" :(', $channel);
}
// parse response a bit. No message yet.
$response = $json['firefly_iii'][$channel];
$return['version'] = $response['version'];
$return['level'] = 'success';
$return['date'] = Carbon::createFromFormat('Y-m-d', $response['date'])->startOfDay();
Log::info('Response from update server', $response);
return $return;
}
}