Skip to content

Commit

Permalink
feat: Handle saving content without an internet connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
owenconti committed May 19, 2020
1 parent ea0f05b commit 5e35ab1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
4 changes: 4 additions & 0 deletions resources/views/partials/_gist_content.antlers.html
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{{ if gist_id == null || gist_filename == null }}
<pre><code>{{ code | noparse | entities }}</code></pre>
{{ else }}
<script src="https://gist.github.com/{{ gist_id }}.js?file={{ gist_filename }}"></script>
{{ /if }}
70 changes: 39 additions & 31 deletions src/Listeners/HandleContentSaving.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OhSeeSoftware\OhSeeGists\Listeners;

use GrahamCampbell\GitHub\GitHubManager;
use Illuminate\Support\Facades\Log;
use Statamic\Events\Data\EntrySaving;

class HandleContentSaving
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down

0 comments on commit 5e35ab1

Please sign in to comment.