mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 04:03:26 +00:00
Code cleanup
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -119,21 +119,21 @@ class AccountUpdateService
|
||||
private function updateAccount(Account $account, array $data): Account
|
||||
{
|
||||
// update the account itself:
|
||||
if(array_key_exists('name', $data)) {
|
||||
$account->name = $data['name'];
|
||||
if (array_key_exists('name', $data)) {
|
||||
$account->name = $data['name'];
|
||||
}
|
||||
if(array_key_exists('active', $data)) {
|
||||
if (array_key_exists('active', $data)) {
|
||||
$account->active = $data['active'];
|
||||
}
|
||||
if(array_key_exists('iban', $data)) {
|
||||
$account->iban = $data['iban'];
|
||||
if (array_key_exists('iban', $data)) {
|
||||
$account->iban = $data['iban'];
|
||||
}
|
||||
|
||||
// set liability, but account must already be a liability.
|
||||
//$liabilityType = $data['liability_type'] ?? '';
|
||||
if ($this->isLiability($account) && array_key_exists('liability_type', $data)) {
|
||||
$type = $this->getAccountType($data['liability_type']);
|
||||
if(null !== $type) {
|
||||
$type = $this->getAccountType($data['liability_type']);
|
||||
if (null !== $type) {
|
||||
$account->account_type_id = $type->id;
|
||||
}
|
||||
}
|
||||
@@ -160,20 +160,6 @@ class AccountUpdateService
|
||||
return in_array($type, [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isLiabilityType(string $type): bool
|
||||
{
|
||||
if ('' === $type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return 1 === AccountType::whereIn('type', [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE])->where('type', ucfirst($type))->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*/
|
||||
@@ -334,4 +320,18 @@ class AccountUpdateService
|
||||
}
|
||||
Log::debug('Account was not marked as inactive, do nothing.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isLiabilityType(string $type): bool
|
||||
{
|
||||
if ('' === $type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return 1 === AccountType::whereIn('type', [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE])->where('type', ucfirst($type))->count();
|
||||
}
|
||||
}
|
||||
|
@@ -30,6 +30,7 @@ use RuntimeException;
|
||||
|
||||
/**
|
||||
* Class PwndVerifierV2.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class PwndVerifierV2 implements Verifier
|
||||
@@ -50,7 +51,7 @@ class PwndVerifierV2 implements Verifier
|
||||
$uri = sprintf('https://api.pwnedpasswords.com/range/%s', $prefix);
|
||||
$opt = [
|
||||
'headers' => [
|
||||
'User-Agent' => 'Firefly III v' . config('firefly.version'),
|
||||
'User-Agent' => 'Firefly III v' . config('firefly.version'),
|
||||
'Add-Padding' => 'true',
|
||||
],
|
||||
'timeout' => 3.1415];
|
||||
@@ -61,7 +62,7 @@ class PwndVerifierV2 implements Verifier
|
||||
try {
|
||||
$client = new Client();
|
||||
$res = $client->request('GET', $uri, $opt);
|
||||
} catch (GuzzleException|Exception $e) {
|
||||
} catch (GuzzleException | Exception $e) {
|
||||
Log::error(sprintf('Could not verify password security: %s', $e->getMessage()));
|
||||
|
||||
return true;
|
||||
|
@@ -68,14 +68,6 @@ class StandardWebhookSender implements WebhookSenderInterface
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setMessage(WebhookMessage $message): void
|
||||
{
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@@ -144,4 +136,12 @@ class StandardWebhookSender implements WebhookSenderInterface
|
||||
Log::debug(sprintf('Webhook request body size: %d bytes', strlen($json)));
|
||||
Log::debug(sprintf('Response body: %s', $res->getBody()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setMessage(WebhookMessage $message): void
|
||||
{
|
||||
$this->message = $message;
|
||||
}
|
||||
}
|
||||
|
@@ -55,13 +55,13 @@ interface WebhookSenderInterface
|
||||
*/
|
||||
public function getVersion(): int;
|
||||
|
||||
/**
|
||||
* @param WebhookMessage $message
|
||||
*/
|
||||
public function setMessage(WebhookMessage $message): void;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function send(): void;
|
||||
|
||||
/**
|
||||
* @param WebhookMessage $message
|
||||
*/
|
||||
public function setMessage(WebhookMessage $message): void;
|
||||
}
|
||||
|
Reference in New Issue
Block a user