mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-11 01:24:46 +02:00
75 lines
2.0 KiB
PHP
75 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Actions\Discord;
|
|
|
|
use App\Models\Discord\DiscordThread;
|
|
use App\Models\Wiki\Anime;
|
|
use Exception;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* Class DiscordThreadAction.
|
|
*/
|
|
class DiscordThreadAction
|
|
{
|
|
/**
|
|
* Handle the action.
|
|
*
|
|
* @param Anime $anime
|
|
* @param array $fields
|
|
* @return Exception|null
|
|
*/
|
|
public function handle(Anime $anime, array $fields): ?Exception
|
|
{
|
|
try {
|
|
$anime->load(Anime::RELATION_IMAGES);
|
|
|
|
$anime->name = Arr::get($fields, 'name');
|
|
|
|
/** @var \Illuminate\Filesystem\FilesystemAdapter */
|
|
$fs = Storage::disk(Config::get('image.disk'));
|
|
|
|
$anime->images->each(fn ($image) => Arr::set($image, 'link', $fs->url($image->path)));
|
|
|
|
$response = Http::withHeaders(['x-api-key' => Config::get('services.discord.api_key')])
|
|
->acceptJson()
|
|
->post(Config::get('services.discord.api_url') . '/thread', $anime->toArray())
|
|
->throw()
|
|
->json();
|
|
|
|
if (Arr::has($response, 'id')) {
|
|
DiscordThread::query()->create([
|
|
DiscordThread::ATTRIBUTE_NAME => Arr::get($response, 'name'),
|
|
DiscordThread::ATTRIBUTE_ID => intval(Arr::get($response, 'id')),
|
|
DiscordThread::ATTRIBUTE_ANIME => $anime->getKey(),
|
|
]);
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch (Exception $e) {
|
|
return $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the thread by ID.
|
|
*
|
|
* @param string $id
|
|
* @return array
|
|
*/
|
|
public function get(string $id): array
|
|
{
|
|
return Http::withHeaders(['x-api-key' => Config::get('services.discord.api_key')])
|
|
->acceptJson()
|
|
->get(Config::get('services.discord.api_url') . '/thread', ['id' => $id])
|
|
->throw()
|
|
->json();
|
|
}
|
|
}
|