Skip to content

Commit

Permalink
Merge pull request #924 from hydephp/refactor-image-models
Browse files Browse the repository at this point in the history
Refactor internal data store of LocalFeaturedImage $source property to not include directory information
  • Loading branch information
caendesilva committed Feb 5, 2023
2 parents e796bb9 + 1c295d5 commit b69fd2e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __toString(): string
*/
abstract public function getSource(): string;

/** Called from constructor to allow child classes to validate and transform the value as needed before assignment. */
protected function setSource(string $source): string
{
return $source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Hyde\Framework\Exceptions\FileNotFoundException;
use Hyde\Hyde;
use InvalidArgumentException;
use function str_starts_with;
use function substr;

Expand All @@ -16,14 +15,15 @@
* The internal data structure forces the image source to reference a file in the _media directory,
* and thus that is what is required for the input. However, when outputting data, the data will
* be used for the _site/media directory, so it will provide data relative to the site root.
*
* The source information is stored in $this->source, which is a file in the _media directory.
*/
class LocalFeaturedImage extends FeaturedImage
{
protected function setSource(string $source): string
{
if (! str_starts_with($source, '_media/')) {
// Throwing an exception here ensures we have a super predictable state.
throw new InvalidArgumentException('LocalFeaturedImage source must start with _media/');
if (str_starts_with($source, '_media/')) {
$source = substr($source, 7);
}

// We could also validate the file exists here if we want. We might also want to just send a warning.
Expand All @@ -33,24 +33,24 @@ protected function setSource(string $source): string

public function getSource(): string
{
// Return value must be relative to the site's root.
return Hyde::relativeLink(substr($this->source, 1));
// Return value is relative to the site's root.
return Hyde::relativeLink("media/$this->source");
}

public function getContentLength(): int
{
return filesize($this->storageValidatedPath());
return filesize($this->validatedStoragePath());
}

protected function storagePath(): string
{
return Hyde::path($this->source);
return Hyde::path("_media/$this->source");
}

protected function storageValidatedPath(): string
protected function validatedStoragePath(): string
{
if (! file_exists($this->storagePath())) {
throw new FileNotFoundException("Image at $this->source does not exist");
throw new FileNotFoundException(sprintf('Image at %s does not exist', Hyde::pathToRelative($this->storagePath())));
}

return $this->storagePath();
Expand Down
9 changes: 0 additions & 9 deletions packages/framework/tests/Feature/FeaturedImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Hyde\Markdown\Models\FrontMatter;
use Hyde\Testing\TestCase;
use Illuminate\Support\Facades\Http;
use InvalidArgumentException;

/**
* @covers \Hyde\Framework\Features\Blogging\Models\FeaturedImage
Expand Down Expand Up @@ -135,14 +134,6 @@ public function testCanConstructLocalFeaturedImage()
$this->assertEquals('media/foo', $image->getSource());
}

public function testCannotConstructLocalFeaturedImageWithInvalidSource()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('LocalFeaturedImage source must start with _media/');

new LocalFeaturedImage('foo', ...$this->defaultArguments());
}

public function testFeaturedImageGetContentLength()
{
$this->file('_media/foo', 'image');
Expand Down

0 comments on commit b69fd2e

Please sign in to comment.