Skip to content

Commit

Permalink
fix: Refactor HandleContentSaving
Browse files Browse the repository at this point in the history
  • Loading branch information
owenconti committed May 18, 2020
1 parent aee99ed commit 0e011e7
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/Listeners/HandleContentSaving.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,46 @@ private function buildGistData(array &$gistBlocks, string $title): array

private function saveGist(array $gistData, array &$gistBlocks): void
{
$gistId = null;
foreach ($gistBlocks as &$gistBlock) {
if (empty($gistId) && !empty($gistBlock['gist_id'])) {
$gistId = $gistBlock['gist_id'];
}
}

if (empty($gistData['files'])) {
return;
}

$gistId = $this->getGistId($gistBlocks);

if (empty($gistId)) {
// Create a new Gist
$response = $this->github->gists()->create($gistData);
$this->createGist($gistData, $gistBlocks);
return;
}

$this->updateGist($gistId, $gistData);
}

foreach ($gistBlocks as &$gistBlock) {
$gistBlock['gist_id'] = $response['id'];
private function getGistId(array $gistBlocks): ?string
{
$gistId = null;
foreach ($gistBlocks as &$gistBlock) {
$gistBlockId = $gistBlock['gist_id'] ?? null;

if (!$gistBlockId || $gistId) {
continue;
}

return;

$gistId = $gistBlockId;
}
return $gistId;
}

private function createGist(array $gistData, array &$gistBlocks)
{
$response = $this->github->gists()->create($gistData);

foreach ($gistBlocks as &$gistBlock) {
$gistBlock['gist_id'] = $response['id'];
}
}

// Update existing Gist
private function updateGist(string $gistId, array $gistData)
{
$this->github->gists()->update($gistId, $gistData);
}

Expand Down

0 comments on commit 0e011e7

Please sign in to comment.