firefly-iii/app/Models/Webhook.php

213 lines
6.5 KiB
PHP
Raw Normal View History

2020-11-29 11:35:49 -06:00
<?php
2021-01-29 11:50:35 -06:00
/*
* Webhook.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-21 22:35:06 -06:00
declare(strict_types=1);
2020-11-29 11:35:49 -06:00
namespace FireflyIII\Models;
2021-08-28 08:47:09 -05:00
2021-05-24 01:50:17 -05:00
use Eloquent;
2022-09-17 00:07:25 -05:00
use FireflyIII\Enums\WebhookDelivery;
use FireflyIII\Enums\WebhookResponse;
use FireflyIII\Enums\WebhookTrigger;
2020-11-29 11:35:49 -06:00
use FireflyIII\User;
2021-05-24 01:50:17 -05:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
2020-11-29 11:35:49 -06:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
2020-11-29 11:35:49 -06:00
use Illuminate\Database\Eloquent\SoftDeletes;
2023-10-30 13:49:40 -05:00
use Carbon\Carbon;
2020-11-29 11:35:49 -06:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
2020-12-03 23:20:44 -06:00
* FireflyIII\Models\Webhook
2020-11-29 11:35:49 -06:00
*
2023-11-04 11:26:38 -05:00
* @property int|string $id
2023-06-21 05:34:58 -05:00
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
* @property int|string $user_id
2023-06-21 05:34:58 -05:00
* @property bool $active
* @property int $trigger
* @property int $response
* @property int $delivery
* @property string $url
* @property-read User $user
2021-05-24 01:50:17 -05:00
* @property-read Collection|WebhookMessage[] $webhookMessages
2023-06-21 05:34:58 -05:00
* @property-read int|null $webhook_messages_count
2021-05-24 01:50:17 -05:00
* @method static Builder|Webhook newModelQuery()
* @method static Builder|Webhook newQuery()
2020-12-03 23:20:44 -06:00
* @method static \Illuminate\Database\Query\Builder|Webhook onlyTrashed()
2021-05-24 01:50:17 -05:00
* @method static Builder|Webhook query()
* @method static Builder|Webhook whereActive($value)
* @method static Builder|Webhook whereCreatedAt($value)
* @method static Builder|Webhook whereDeletedAt($value)
* @method static Builder|Webhook whereDelivery($value)
* @method static Builder|Webhook whereId($value)
* @method static Builder|Webhook whereResponse($value)
* @method static Builder|Webhook whereTrigger($value)
* @method static Builder|Webhook whereUpdatedAt($value)
* @method static Builder|Webhook whereUrl($value)
* @method static Builder|Webhook whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|Webhook withTrashed()
* @method static \Illuminate\Database\Query\Builder|Webhook withoutTrashed()
2023-06-21 05:34:58 -05:00
* @property string $title
* @property string $secret
2021-05-24 01:50:17 -05:00
* @method static Builder|Webhook whereSecret($value)
* @method static Builder|Webhook whereTitle($value)
2023-06-21 05:34:58 -05:00
* @property int|null $user_group_id
2021-09-18 03:08:10 -05:00
* @method static Builder|Webhook whereUserGroupId($value)
2023-03-08 23:33:23 -06:00
* @mixin Eloquent
2020-11-29 11:35:49 -06:00
*/
class Webhook extends Model
{
use SoftDeletes;
2020-11-29 11:35:49 -06:00
protected $casts
= [
2020-11-29 11:35:49 -06:00
'active' => 'boolean',
'trigger' => 'integer',
'response' => 'integer',
'delivery' => 'integer',
];
protected $fillable = ['active', 'trigger', 'response', 'delivery', 'user_id', 'user_group_id', 'url', 'title', 'secret'];
2020-11-29 11:35:49 -06:00
2022-09-17 00:07:25 -05:00
/**
* @return array
*/
2022-12-29 12:42:26 -06:00
public static function getDeliveries(): array
2022-09-17 00:07:25 -05:00
{
$array = [];
2022-12-29 12:42:26 -06:00
$set = WebhookDelivery::cases();
2022-09-17 00:07:25 -05:00
foreach ($set as $item) {
$array[$item->value] = $item->name;
}
return $array;
}
2022-09-18 03:45:38 -05:00
/**
* @return array
*/
2022-12-29 12:42:26 -06:00
public static function getDeliveriesForValidation(): array
2022-09-18 03:45:38 -05:00
{
$array = [];
2022-12-29 12:42:26 -06:00
$set = WebhookDelivery::cases();
2022-09-18 03:45:38 -05:00
foreach ($set as $item) {
2022-12-29 12:42:26 -06:00
$array[$item->name] = $item->value;
2022-09-18 03:45:38 -05:00
$array[$item->value] = $item->value;
}
return $array;
}
/**
* @return array
*/
2022-12-29 12:42:26 -06:00
public static function getResponses(): array
2022-09-18 03:45:38 -05:00
{
$array = [];
$set = WebhookResponse::cases();
foreach ($set as $item) {
2022-12-29 12:42:26 -06:00
$array[$item->value] = $item->name;
2022-09-18 03:45:38 -05:00
}
return $array;
}
/**
* @return array
*/
2022-12-29 12:42:26 -06:00
public static function getResponsesForValidation(): array
2022-09-18 03:45:38 -05:00
{
$array = [];
2022-12-29 12:42:26 -06:00
$set = WebhookResponse::cases();
2022-09-18 03:45:38 -05:00
foreach ($set as $item) {
2022-12-29 12:42:26 -06:00
$array[$item->name] = $item->value;
2022-09-18 03:45:38 -05:00
$array[$item->value] = $item->value;
}
return $array;
}
2022-09-17 00:07:25 -05:00
/**
* @return array
*/
2022-12-29 12:42:26 -06:00
public static function getTriggers(): array
2022-09-17 00:07:25 -05:00
{
$array = [];
2022-12-29 12:42:26 -06:00
$set = WebhookTrigger::cases();
2022-09-17 00:07:25 -05:00
foreach ($set as $item) {
$array[$item->value] = $item->name;
}
return $array;
}
/**
* @return array
*/
2022-12-29 12:42:26 -06:00
public static function getTriggersForValidation(): array
2022-09-17 00:07:25 -05:00
{
$array = [];
2022-12-29 12:42:26 -06:00
$set = WebhookTrigger::cases();
2022-09-17 00:07:25 -05:00
foreach ($set as $item) {
2022-12-29 12:42:26 -06:00
$array[$item->name] = $item->value;
$array[$item->value] = $item->value;
2022-09-17 00:07:25 -05:00
}
return $array;
}
2022-12-29 12:42:26 -06:00
/**
* Route binder. Converts the key in the URL to the specified object (or throw 404).
*
2023-06-21 05:34:58 -05:00
* @param string $value
2022-12-29 12:42:26 -06:00
*
* @return Webhook
* @throws NotFoundHttpException
*/
2023-11-04 08:18:49 -05:00
public static function routeBinder(string $value): self
2022-12-29 12:42:26 -06:00
{
if (auth()->check()) {
$webhookId = (int)$value;
/** @var User $user */
$user = auth()->user();
2023-11-05 09:55:16 -06:00
/** @var Webhook|null $webhook */
2022-12-29 12:42:26 -06:00
$webhook = $user->webhooks()->find($webhookId);
if (null !== $webhook) {
return $webhook;
}
}
throw new NotFoundHttpException();
}
/**
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* @return HasMany
*/
public function webhookMessages(): HasMany
{
return $this->hasMany(WebhookMessage::class);
}
2020-12-21 22:35:06 -06:00
}