Passed
Push — master ( 0e441f...0d5f72 )
by Caen
03:21 queued 14s
created

LocalFeaturedImage::replaceSlashes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Blogging\Models;
6
7
use Hyde\Framework\Exceptions\FileNotFoundException;
8
use Hyde\Hyde;
9
use Illuminate\Support\Str;
10
11
/**
12
 * A featured image object, for a file stored locally.
13
 *
14
 * The internal data structure forces the image source to reference a file in the _media directory,
15
 * and thus that is what is required for the input. However, when outputting data, the data will
16
 * be used for the _site/media directory, so it will provide data relative to the site root.
17
 *
18
 * The source information is stored in $this->source, which is a file in the _media directory.
19
 */
20
class LocalFeaturedImage extends FeaturedImage
21
{
22
    protected function setSource(string $source): string
23
    {
24
        // We could also validate the file exists here if we want.
25
        // We might also want to just send a warning. But for now,
26
        // we'll just trim any leading media path prefixes.
27
28
        return Str::after($source, Hyde::getMediaDirectory().'/');
29
    }
30
31
    public function getSource(): string
32
    {
33
        // Return value is always resolvable from a compiled page in the _site directory.
34
        return Hyde::mediaLink($this->source);
35
    }
36
37
    public function getContentLength(): int
38
    {
39
        return filesize($this->validatedStoragePath());
40
    }
41
42
    protected function storagePath(): string
43
    {
44
        return Hyde::mediaPath($this->source);
45
    }
46
47
    protected function validatedStoragePath(): string
48
    {
49
        if (! file_exists($this->storagePath())) {
50
            throw new FileNotFoundException(sprintf('Image at %s does not exist', Hyde::pathToRelative($this->storagePath())));
51
        }
52
53
        return $this->storagePath();
54
    }
55
}
56