Skip to content

Commit

Permalink
Remove duplicate parsing from service that's also handled in action
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Dec 20, 2022
1 parent 0009499 commit 55615d7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Features\Publications;

use Hyde\Framework\Actions\SourceFileParser;
use function basename;
use function dirname;
use Exception;
Expand Down Expand Up @@ -102,17 +103,7 @@ public static function getValuesForTagName(string $tagName): ?Collection
*/
public static function parsePublicationFile(string $identifier): PublicationPage
{
$identifier = Str::replaceLast('.md', '', $identifier);
$fileData = static::getFileData("$identifier.md");

$parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData);

return new PublicationPage(
identifier: basename($identifier),
matter: $parsedFileData->matter(),
markdown: $parsedFileData->body(),
type: PublicationType::get(dirname($identifier))
);
return (new SourceFileParser(PublicationPage::class, Str::replaceLast('.md', '', $identifier)))->get();
}

/**
Expand All @@ -123,20 +114,6 @@ public static function publicationTypeExists(string $pubTypeName): bool
return static::getPublicationTypes()->has(Str::slug($pubTypeName));
}

/**
* @throws \Safe\Exceptions\FilesystemException
* @throws \Exception If the file could not be read.
*/
protected static function getFileData(string $filepath): string
{
$fileData = file_get_contents(Hyde::path($filepath));
if (! $fileData) {
throw new Exception("No data read from [$filepath]");
}

return $fileData;
}

protected static function getSchemaFiles(): array
{
return glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Testing\Feature\Services;

use Hyde\Framework\Exceptions\FileNotFoundException;
use function copy;
use ErrorException;
use Exception;
Expand Down Expand Up @@ -135,19 +136,8 @@ public function testParsePublicationFileWithNonExistentFile()
{
$this->createPublicationType();

$this->expectException(ErrorException::class);
$this->expectExceptionMessage('Failed to open stream: No such file or directory');

PublicationService::parsePublicationFile('test-publication/foo');
}

public function testParsePublicationFileWithInvalidFile()
{
$this->createPublicationType();
file_put_contents(Hyde::path('test-publication/foo.md'), '');

$this->expectException(Exception::class);
$this->expectExceptionMessage('No data read from [test-publication/foo.md]');
$this->expectException(FileNotFoundException::class);
$this->expectExceptionMessage('File [test-publication/foo.md] not found.');

PublicationService::parsePublicationFile('test-publication/foo');
}
Expand Down

0 comments on commit 55615d7

Please sign in to comment.