Passed
Push — master ( e3b121...219a49 )
by Caen
03:57 queued 14s
created

LocalFeaturedImage::validatedStoragePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 1
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', $this->replaceSlashes(Hyde::pathToRelative($this->storagePath()))));
0 ignored issues
show
Deprecated Code introduced by
The function Hyde\Framework\Features\...Image::replaceSlashes() has been deprecated: Slashes may be normalized before reaching here, see e3b121b4ad ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

50
            throw new FileNotFoundException(sprintf('Image at %s does not exist', /** @scrutinizer ignore-deprecated */ $this->replaceSlashes(Hyde::pathToRelative($this->storagePath()))));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
51
        }
52
53
        return $this->storagePath();
54
    }
55
56
    /** @deprecated Slashes may be normalized before reaching here, see e3b121b4ad */
57
    private function replaceSlashes(string $path): string
58
    {
59
        return str_replace('\\', '/', $path);
60
    }
61
}
62