Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 25, 2022
1 parent 6a45a0d commit f8452b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 62 deletions.
71 changes: 11 additions & 60 deletions src/Actions/CreatesNewPageSourceFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Hyde\Framework\Actions;

use Exception;
use Hyde\Framework\Exceptions\FileConflictException;
use Hyde\Framework\Exceptions\UnsupportedPageTypeException;
use Hyde\Framework\Hyde;
Expand All @@ -18,32 +17,10 @@
*/
class CreatesNewPageSourceFile
{
/**
* The Page title.
*
* @var string
*/
public string $title;

/**
* The Page slug.
*/
public string $slug;
public string $outputPath;

/**
* The file path.
*/
public string $path;

/**
* Construct the class.
*
* @param string $title - The page title, will be used to generate the slug
* @param string $type - The page type, FQDN of the page class
* @param bool $force - Overwrite any existing files?
*
* @throws Exception if the page type is not supported or the file already exists
*/
public function __construct(string $title, string $type = MarkdownPage::class, public bool $force = false)
{
$this->title = $title;
Expand All @@ -52,25 +29,14 @@ public function __construct(string $title, string $type = MarkdownPage::class, p
$this->createPage($type);
}

/**
* Check if the file can be saved.
*
* @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 FileConflictException($path);
}
}

/**
* Create the page.
*
* @param string $type FQDN of the page class
*
* @throws UnsupportedPageTypeException if the page type is not supported
*/
public function createPage(string $type): int|false
{
if ($type === MarkdownPage::class) {
Expand All @@ -87,36 +53,26 @@ public function createPage(string $type): int|false
throw new UnsupportedPageTypeException('The page type must be either "markdown", "blade", or "documentation"');
}

/**
* Create the Markdown file.
*
* @throws FileConflictException if the file cannot be saved.
*/
public function createMarkdownFile(): int|false
{
$this->path = Hyde::path("_pages/$this->slug.md");
$this->outputPath = Hyde::path("_pages/$this->slug.md");

$this->canSaveFile($this->path);
$this->canSaveFile($this->outputPath);

return file_put_contents(
$this->path,
$this->outputPath,
"---\ntitle: $this->title\n---\n\n# $this->title\n"
);
}

/**
* Create the Blade file.
*
* @throws FileConflictException if the file cannot be saved.
*/
public function createBladeFile(): int|false
{
$this->path = Hyde::path("_pages/$this->slug.blade.php");
$this->outputPath = Hyde::path("_pages/$this->slug.blade.php");

$this->canSaveFile($this->path);
$this->canSaveFile($this->outputPath);

return file_put_contents(
$this->path,
$this->outputPath,
<<<EOF
@extends('hyde::layouts.app')
@section('content')
Expand All @@ -132,19 +88,14 @@ public function createBladeFile(): int|false
);
}

/**
* Create the Documentation file.
*
* @throws FileConflictException if the file cannot be saved.
*/
public function createDocumentationFile(): int|false
{
$this->path = Hyde::path("_docs/$this->slug.md");
$this->outputPath = Hyde::path("_docs/$this->slug.md");

$this->canSaveFile($this->path);
$this->canSaveFile($this->outputPath);

return file_put_contents(
$this->path,
$this->outputPath,
"# $this->title\n"
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Actions/CreatesNewPageSourceFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ public function test_that_the_file_path_can_be_returned()
{
$this->assertEquals(
Hyde::path('_pages/682072b-test-page.md'),
(new CreatesNewPageSourceFile('682072b Test Page'))->path
(new CreatesNewPageSourceFile('682072b Test Page'))->outputPath
);

$this->assertEquals(
Hyde::path('_pages/682072b-test-page.blade.php'),
(new CreatesNewPageSourceFile('682072b Test Page', BladePage::class))->path
(new CreatesNewPageSourceFile('682072b Test Page', BladePage::class))->outputPath
);
}
}

0 comments on commit f8452b9

Please sign in to comment.