firefly-iii/app/Models/WebhookAttempt.php

110 lines
4.0 KiB
PHP
Raw Normal View History

2020-12-03 23:20:44 -06:00
<?php
2021-01-29 11:50:35 -06:00
/*
* WebhookAttempt.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-12-03 23:20:44 -06:00
namespace FireflyIII\Models;
2021-05-24 01:06:56 -05:00
2023-11-05 12:41:37 -06:00
use Carbon\Carbon;
2021-05-24 01:50:17 -05:00
use Eloquent;
2021-03-07 05:13:22 -06:00
use FireflyIII\User;
2023-11-05 12:41:37 -06:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2020-12-03 23:20:44 -06:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2021-03-07 05:13:22 -06:00
use Illuminate\Database\Eloquent\SoftDeletes;
2021-05-24 01:50:17 -05:00
use Illuminate\Database\Query\Builder;
2021-03-07 05:13:22 -06:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2023-11-05 12:41:37 -06:00
use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
2020-12-03 23:20:44 -06:00
/**
* Class WebhookAttempt
2021-01-31 00:26:52 -06:00
*
2023-11-05 12:41:37 -06:00
* @property int $id
2023-06-21 05:34:58 -05:00
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property string|null $deleted_at
2023-11-05 12:41:37 -06:00
* @property int $webhook_message_id
* @property int|string $status_code
2023-06-21 05:34:58 -05:00
* @property string|null $logs
* @property string|null $response
2021-05-24 01:50:17 -05:00
* @property-read WebhookMessage $webhookMessage
2021-01-31 00:26:52 -06:00
* @method static \Illuminate\Database\Eloquent\Builder|WebhookAttempt newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|WebhookAttempt newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|WebhookAttempt query()
* @method static \Illuminate\Database\Eloquent\Builder|WebhookAttempt whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebhookAttempt whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebhookAttempt whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebhookAttempt whereLogs($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebhookAttempt whereResponse($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebhookAttempt whereStatusCode($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebhookAttempt whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebhookAttempt whereWebhookMessageId($value)
2021-05-24 01:50:17 -05:00
* @method static Builder|WebhookAttempt onlyTrashed()
* @method static Builder|WebhookAttempt withTrashed()
* @method static Builder|WebhookAttempt withoutTrashed()
2023-03-08 23:33:23 -06:00
* @mixin Eloquent
2020-12-03 23:20:44 -06:00
*/
class WebhookAttempt extends Model
{
2023-11-05 12:41:37 -06:00
use ReturnsIntegerIdTrait;
2021-03-07 05:13:22 -06:00
use SoftDeletes;
2021-05-24 01:06:56 -05:00
2021-03-07 05:13:22 -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
2021-03-07 05:13:22 -06:00
*
* @return WebhookAttempt
* @throws NotFoundHttpException
*/
2023-11-04 08:18:49 -05:00
public static function routeBinder(string $value): self
2021-03-07 05:13:22 -06:00
{
if (auth()->check()) {
2022-12-29 12:42:26 -06:00
$attemptId = (int)$value;
2021-03-07 05:13:22 -06:00
/** @var User $user */
$user = auth()->user();
2023-11-05 09:55:16 -06:00
/** @var WebhookAttempt|null $attempt */
2021-03-07 05:13:22 -06:00
$attempt = self::find($attemptId);
2021-05-24 01:06:56 -05:00
if (null !== $attempt && $attempt->webhookMessage->webhook->user_id === $user->id) {
return $attempt;
2021-03-07 05:13:22 -06:00
}
}
2022-10-30 08:24:28 -05:00
throw new NotFoundHttpException();
2021-03-07 05:13:22 -06:00
}
2021-08-28 08:47:09 -05:00
/**
* @return BelongsTo
*/
public function webhookMessage(): BelongsTo
{
return $this->belongsTo(WebhookMessage::class);
}
2023-11-05 12:41:37 -06:00
/**
* @return Attribute
*/
protected function webhookMessageId(): Attribute
{
return Attribute::make(
get: static fn($value) => (int)$value,
);
}
2020-12-21 22:35:06 -06:00
}