Files
firefly-iii/app/Services/Webhook/StandardWebhookSender.php

152 lines
5.8 KiB
PHP
Raw Normal View History

<?php
2021-01-29 18:50:35 +01:00
/*
* StandardWebhookSender.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-12-22 05:35:06 +01:00
declare(strict_types=1);
namespace FireflyIII\Services\Webhook;
2021-11-19 15:47:19 +01:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Webhook\SignatureGeneratorInterface;
2021-01-15 21:01:53 +01:00
use FireflyIII\Models\WebhookAttempt;
use FireflyIII\Models\WebhookMessage;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
2023-12-30 12:54:21 +01:00
use GuzzleHttp\Exception\GuzzleException;
2021-04-06 17:00:16 +02:00
use GuzzleHttp\Exception\RequestException;
2020-12-05 07:01:26 +01:00
/**
* Class StandardWebhookSender
*/
class StandardWebhookSender implements WebhookSenderInterface
{
2020-12-05 07:01:26 +01:00
private WebhookMessage $message;
private int $version = 1;
public function getVersion(): int
{
return $this->version;
}
2023-12-22 17:28:42 +01:00
/**
2023-12-30 12:54:21 +01:00
* @throws GuzzleException
2023-12-22 17:28:42 +01:00
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function send(): void
{
// have the signature generator generate a signature. If it fails, the error thrown will
// end up in send() to be caught.
2023-02-01 18:05:52 +01:00
$signatureGenerator = app(SignatureGeneratorInterface::class);
2023-01-05 19:05:23 +01:00
$this->message->sent = true;
$this->message->save();
2023-12-20 19:35:52 +01:00
2021-11-19 15:47:19 +01:00
try {
$signature = $signatureGenerator->generate($this->message);
2022-03-29 14:59:58 +02:00
} catch (FireflyException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error('Did not send message because of a Firefly III Exception.');
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
$attempt = new WebhookAttempt();
2021-11-19 15:47:19 +01:00
$attempt->webhookMessage()->associate($this->message);
$attempt->status_code = 0;
$attempt->logs = sprintf('Exception: %s', $e->getMessage());
2021-11-19 15:47:19 +01:00
$attempt->save();
$this->message->errored = true;
$this->message->sent = false;
$this->message->save();
return;
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Trying to send webhook message #%d', $this->message->id));
try {
2020-12-05 07:01:26 +01:00
$json = json_encode($this->message->message, JSON_THROW_ON_ERROR);
2023-12-20 19:35:52 +01:00
} catch (\JsonException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error('Did not send message because of a JSON error.');
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
$attempt = new WebhookAttempt();
2021-01-15 21:01:53 +01:00
$attempt->webhookMessage()->associate($this->message);
$attempt->status_code = 0;
$attempt->logs = sprintf('Json error: %s', $e->getMessage());
2021-01-15 21:01:53 +01:00
$attempt->save();
$this->message->errored = true;
$this->message->sent = false;
$this->message->save();
return;
}
$options = [
'body' => $json,
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Signature' => $signature,
'connect_timeout' => 3.14,
'User-Agent' => sprintf('FireflyIII/%s', config('firefly.version')),
'timeout' => 10,
],
];
$client = new Client();
2023-12-20 19:35:52 +01:00
try {
2023-02-01 18:05:52 +01:00
$res = $client->request('POST', $this->message->webhook->url, $options);
2023-12-20 19:35:52 +01:00
} catch (ConnectException|RequestException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error('The webhook could NOT be submitted but Firefly III caught the error below.');
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
2021-03-07 12:13:22 +01:00
$logs = sprintf("%s\n%s", $e->getMessage(), $e->getTraceAsString());
2021-03-07 12:13:22 +01:00
2020-12-05 07:01:26 +01:00
$this->message->errored = true;
$this->message->sent = false;
2021-01-15 21:01:53 +01:00
$this->message->save();
2021-03-07 12:13:22 +01:00
$attempt = new WebhookAttempt();
2021-03-07 12:13:22 +01:00
$attempt->webhookMessage()->associate($this->message);
$attempt->status_code = 0;
2023-02-01 18:05:52 +01:00
if (method_exists($e, 'hasResponse') && method_exists($e, 'getResponse')) {
$attempt->status_code = $e->hasResponse() ? $e->getResponse()->getStatusCode() : 0;
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('The status code of the error response is: %d', $attempt->status_code));
2024-12-22 08:43:12 +01:00
$body = (string) ($e->hasResponse() ? $e->getResponse()->getBody() : '');
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('The body of the error response is: %s', $body));
2023-02-01 18:05:52 +01:00
}
$attempt->logs = $logs;
2021-03-07 12:13:22 +01:00
$attempt->save();
2021-01-15 21:01:53 +01:00
return;
}
2023-01-05 19:05:23 +01:00
$this->message->sent = true;
2020-12-05 07:01:26 +01:00
$this->message->save();
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Webhook message #%d was sent. Status code %d', $this->message->id, $res->getStatusCode()));
app('log')->debug(sprintf('Webhook request body size: %d bytes', strlen($json)));
app('log')->debug(sprintf('Response body: %s', $res->getBody()));
}
2021-03-21 09:15:40 +01:00
public function setMessage(WebhookMessage $message): void
{
$this->message = $message;
}
2020-12-22 05:35:06 +01:00
}