Deploy HickLab3D Dashboard / Deploy to production (push) Successful in 25s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
61 lines
2.1 KiB
PHP
61 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Services\Bambuddy;
|
|
use Closure;
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
use Illuminate\Http\Client\RequestException;
|
|
use Illuminate\View\View;
|
|
use RuntimeException;
|
|
use Throwable;
|
|
|
|
class BambuddyController extends Controller
|
|
{
|
|
public function __invoke(Bambuddy $bambuddy): View
|
|
{
|
|
$printers = [];
|
|
$integrationError = null;
|
|
|
|
try {
|
|
$printers = $bambuddy->printers();
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$integrationError = 'BamBuddy is unavailable. '.$this->failureReason($exception);
|
|
}
|
|
|
|
$queue = $printers === [] ? [] : $this->optional(fn (): array => $bambuddy->queue());
|
|
$recentPrints = $printers === [] ? [] : $this->optional(fn (): array => $bambuddy->recentPrints());
|
|
$stats = $printers === [] ? [] : $this->optional(fn (): array => $bambuddy->stats());
|
|
|
|
return view('printers.index', compact('printers', 'integrationError', 'queue', 'recentPrints', 'stats') + ['bambuddyUrl' => $bambuddy->url()]);
|
|
}
|
|
|
|
private function failureReason(Throwable $exception): string
|
|
{
|
|
return match (true) {
|
|
$exception instanceof RequestException => 'BamBuddy answered HTTP '.$exception->response->status().($exception->response->status() === 401 || $exception->response->status() === 403 ? ' — check the API key.' : '.'),
|
|
$exception instanceof ConnectionException => 'The dashboard server could not connect: '.str($exception->getMessage())->limit(160),
|
|
$exception instanceof RuntimeException => $exception->getMessage(),
|
|
default => 'Check its API key and connection.',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Load secondary page data without letting a failure hide the printers.
|
|
*
|
|
* @param Closure(): array<mixed> $load
|
|
* @return array<mixed>
|
|
*/
|
|
private function optional(Closure $load): array
|
|
{
|
|
try {
|
|
return $load();
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
|
|
return [];
|
|
}
|
|
}
|
|
}
|