1<?php
2
3namespace ComboStrap;
4
5/**
6 *
7 * This class represents a fetcher that sends back a local image processed or not.
8 *
9 */
10abstract class IFetcherLocalImage extends FetcherImage implements IFetcherSource
11{
12
13    /**
14     * @param WikiPath $path
15     * @return IFetcherLocalImage
16     * @throws ExceptionBadArgument - if the path is not an image
17     * @throws ExceptionBadSyntax
18     * @throws ExceptionNotExists
19     */
20    public static function createImageFetchFromPath(WikiPath $path): IFetcherLocalImage
21    {
22
23        try {
24            $mime = FileSystems::getMime($path);
25        } catch (ExceptionNotFound $e) {
26            throw new ExceptionBadArgument("The file ($path) has an unknown mime, we can't verify if we support it", FetcherImage::CANONICAL);
27        }
28
29        if (!$mime->isImage()) {
30            throw new ExceptionBadArgument("The file ($path) has not been detected as being an image, media returned", FetcherImage::CANONICAL);
31        }
32
33        if ($mime->toString() === Mime::SVG) {
34
35            $image = FetcherSvg::createSvgFromPath($path);
36
37        } else {
38
39            $image = FetcherRaster::createImageRasterFetchFromPath($path);
40
41        }
42
43        return $image;
44
45
46    }
47
48    /**
49     * @param string $imageId
50     * @param string|null $rev
51     * @return IFetcherLocalImage
52     * @throws ExceptionBadArgument - if the path is not an image
53     */
54    public static function createImageFetchFromId(string $imageId, string $rev = null): IFetcherLocalImage
55    {
56        $dokuPath = WikiPath::createMediaPathFromId($imageId, $rev);
57        return IFetcherLocalImage::createImageFetchFromPath($dokuPath);
58    }
59
60
61    function getBuster(): string
62    {
63        try {
64            return FileSystems::getCacheBuster($this->getSourcePath());
65        } catch (ExceptionNotFound $e) {
66            // file does not exists
67            return strval((new \DateTime())->getTimestamp());
68        }
69    }
70
71    public function getLabel(): string
72    {
73
74        $sourcePath = $this->getSourcePath();
75        return ResourceName::getFromPath($sourcePath);
76
77    }
78
79
80}
81