Skip to content

Commit

Permalink
Create FileConflictException
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 25, 2022
1 parent 03fcecb commit 02d534c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/Actions/CreatesNewMarkdownPostFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Hyde\Framework\Actions;

use Exception;
use Hyde\Framework\Exceptions\FileConflictException;
use Hyde\Framework\Hyde;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -95,14 +95,14 @@ public function __construct(
* @param bool $force Should the file be created even if a file with the same path already exists?
* @return string|false Returns the path to the file if successful, or false if the file could not be saved.
*
* @throws Exception if a file with the same slug already exists and the force flag is not set.
* @throws FileConflictException if a file with the same slug already exists and the force flag is not set.
*/
public function save(bool $force = false): string|false
{
$path = Hyde::path("_posts/$this->slug.md");

if ($force !== true && file_exists($path)) {
throw new Exception("File at $path already exists! ", 409);
throw new FileConflictException($path);
}

$arrayWithoutSlug = ((array) $this);
Expand Down
11 changes: 6 additions & 5 deletions src/Actions/CreatesNewPageSourceFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Hyde\Framework\Actions;

use Exception;
use Hyde\Framework\Exceptions\FileConflictException;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\BladePage;
use Hyde\Framework\Models\DocumentationPage;
Expand Down Expand Up @@ -53,12 +54,12 @@ public function __construct(string $title, string $type = MarkdownPage::class, p
/**
* Check if the file can be saved.
*
* @throws Exception if the file already exists and cannot be overwritten
* @throws FileConflictException if the file already exists and cannot be overwritten
*/
public function canSaveFile(string $path): void
{
if (file_exists($path) && ! $this->force) {
throw new Exception("File $path already exists!", 409);
throw new FileConflictException($path);
}
}

Expand Down Expand Up @@ -88,7 +89,7 @@ public function createPage(string $type): int|false
/**
* Create the Markdown file.
*
* @throws Exception if the file cannot be saved.
* @throws FileConflictException if the file cannot be saved.
*/
public function createMarkdownFile(): int|false
{
Expand All @@ -105,7 +106,7 @@ public function createMarkdownFile(): int|false
/**
* Create the Blade file.
*
* @throws Exception if the file cannot be saved.
* @throws FileConflictException if the file cannot be saved.
*/
public function createBladeFile(): int|false
{
Expand Down Expand Up @@ -133,7 +134,7 @@ public function createBladeFile(): int|false
/**
* Create the Documentation file.
*
* @throws Exception if the file cannot be saved.
* @throws FileConflictException if the file cannot be saved.
*/
public function createDocumentationFile(): int|false
{
Expand Down
16 changes: 16 additions & 0 deletions src/Exceptions/FileConflictException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Hyde\Framework\Exceptions;

use Exception;

class FileConflictException extends Exception
{
protected $message = 'A file already exists at this path.';
protected $code = 409;

public function __construct(?string $path = null)
{
$this->message = $path ? "File already exists: {$path}" : $this->message;
}
}
5 changes: 3 additions & 2 deletions tests/Feature/Actions/CreatesNewPageSourceFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Hyde\Framework\Actions\CreatesNewPageSourceFile;
use Hyde\Framework\Exceptions\FileConflictException;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\BladePage;
use Hyde\Framework\Models\DocumentationPage;
Expand Down Expand Up @@ -56,8 +57,8 @@ public function test_that_an_exception_is_thrown_if_file_already_exists_and_over
$path = Hyde::path('_pages/foo.md');
file_put_contents($path, 'foo');

$this->expectException(Exception::class);
$this->expectExceptionMessage("File $path already exists!");
$this->expectException(FileConflictException::class);
$this->expectExceptionMessage("File already exists: $path");
$this->expectExceptionCode(409);

new CreatesNewPageSourceFile('foo');
Expand Down
5 changes: 3 additions & 2 deletions tests/Feature/Commands/HydeMakePageCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Feature\Commands;

use Exception;
use Hyde\Framework\Exceptions\FileConflictException;
use Hyde\Framework\Hyde;
use Tests\TestCase;

Expand Down Expand Up @@ -102,8 +103,8 @@ public function test_command_fails_if_file_already_exists()
{
file_put_contents($this->markdownPath, 'This should not be overwritten');

$this->expectException(Exception::class);
$this->expectExceptionMessage("File $this->markdownPath already exists!");
$this->expectException(FileConflictException::class);
$this->expectExceptionMessage("File already exists: $this->markdownPath");
$this->expectExceptionCode(409);
$this->artisan('make:page "8450de2 test page"')->assertExitCode(409);

Expand Down

0 comments on commit 02d534c

Please sign in to comment.