From 5e35ab1231338e322dc0011a138bf31a69412411 Mon Sep 17 00:00:00 2001 From: Owen Conti Date: Mon, 18 May 2020 19:21:08 -0600 Subject: [PATCH] feat: Handle saving content without an internet connection. --- .../views/partials/_gist_content.antlers.html | 4 ++ src/Listeners/HandleContentSaving.php | 70 +++++++++++-------- src/ServiceProvider.php | 10 +-- 3 files changed, 45 insertions(+), 39 deletions(-) diff --git a/resources/views/partials/_gist_content.antlers.html b/resources/views/partials/_gist_content.antlers.html index 2f11267..a5a4e82 100644 --- a/resources/views/partials/_gist_content.antlers.html +++ b/resources/views/partials/_gist_content.antlers.html @@ -1 +1,5 @@ +{{ if gist_id == null || gist_filename == null }} +
{{ code | noparse | entities }}
+{{ else }} +{{ /if }} diff --git a/src/Listeners/HandleContentSaving.php b/src/Listeners/HandleContentSaving.php index d67c180..73d0722 100644 --- a/src/Listeners/HandleContentSaving.php +++ b/src/Listeners/HandleContentSaving.php @@ -3,6 +3,7 @@ namespace OhSeeSoftware\OhSeeGists\Listeners; use GrahamCampbell\GitHub\GitHubManager; +use Illuminate\Support\Facades\Log; use Statamic\Events\Data\EntrySaving; class HandleContentSaving @@ -21,21 +22,26 @@ public function handle(EntrySaving $event) return; } - $data = $event->data->data(); - $content = $data->get('content', []); + try { + $data = $event->data->data(); + $content = $data->get('content', []); - $gistBlocks = $this->getGistBlocks($content); - - if (empty($gistBlocks)) { - return; + $gistBlocks = $this->getGistBlocks($content); + + if (empty($gistBlocks)) { + return; + } + + $title = $data->get('title', 'Created by Oh See Gists add-on'); + + $gistData = $this->buildGistData($gistBlocks, $title); + $this->saveGist($gistData, $gistBlocks); + + $data->put('content', $content); + } catch (\Throwable $e) { + Log::error("Error saving gist blocks"); + Log::error($e); } - - $title = $data['title'] ?? 'Created by Oh See Gists add-on'; - - $gistData = $this->buildGistData($gistBlocks, $title); - $this->saveGist($gistData, $gistBlocks); - - $data->put('content', $content); } private function buildGistData(array &$gistBlocks, string $title): array @@ -69,43 +75,45 @@ private function saveGist(array $gistData, array &$gistBlocks): void return; } - $gistId = $this->getGistId($gistBlocks); + $gistId = $this->getGistIdFromBlocks($gistBlocks); if (empty($gistId)) { - $this->createGist($gistData, $gistBlocks); - return; + $response = $this->createGist($gistData, $gistBlocks); + } else { + $response = $this->updateGist($gistId, $gistData); } - $this->updateGist($gistId, $gistData); + $this->updateGistIds($response['id'], $gistBlocks); } - private function getGistId(array $gistBlocks): ?string + private function getGistIdFromBlocks(array $gistBlocks): ?string { - $gistId = null; foreach ($gistBlocks as &$gistBlock) { $gistBlockId = $gistBlock['gist_id'] ?? null; - if (!$gistBlockId || $gistId) { - continue; + if ($gistBlockId) { + return $gistBlockId; } - - $gistId = $gistBlockId; } - return $gistId; + + return null; } - private function createGist(array $gistData, array &$gistBlocks) + private function createGist(array $gistData, array &$gistBlocks): array { - $response = $this->github->gists()->create($gistData); + return $this->github->gists()->create($gistData); + } - foreach ($gistBlocks as &$gistBlock) { - $gistBlock['gist_id'] = $response['id']; - } + private function updateGist(string $gistId, array $gistData): array + { + return $this->github->gists()->update($gistId, $gistData); } - private function updateGist(string $gistId, array $gistData) + private function updateGistIds(string $gistId, array &$gistBlocks): void { - $this->github->gists()->update($gistId, $gistData); + foreach ($gistBlocks as &$gistBlock) { + $gistBlock['gist_id'] = $gistId; + } } private function getGistBlocks(array &$content): array diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index b76619f..71a9918 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -18,14 +18,8 @@ public function register() { if ($this->app->runningInConsole()) { $this->publishes([ - __DIR__.'/../config/github.php' => config_path('github.php') - ], 'oh-see-gists'); - - $this->publishes([ - __DIR__.'/../resources/fieldsets' => resource_path('fieldsets') - ], 'oh-see-gists'); - - $this->publishes([ + __DIR__.'/../config/github.php' => config_path('github.php'), + __DIR__.'/../resources/fieldsets' => resource_path('fieldsets'), __DIR__.'/../resources/views' => resource_path('views') ], 'oh-see-gists'); }