1<?php
2
3namespace ComboStrap;
4
5use ComboStrap\Web\Url;
6
7/**
8 * Static class of the fetcher system
9 */
10class FetcherSystem
11{
12
13    /**
14     *
15     * @param Url $fetchUrl
16     * @return IFetcherPath
17     * @throws ExceptionBadArgument
18     * @throws ExceptionNotFound
19     * @throws ExceptionInternal
20     */
21    public static function createPathFetcherFromUrl(Url $fetchUrl): IFetcherPath
22    {
23
24        try {
25            $fetcherName = $fetchUrl->getQueryPropertyValue(IFetcher::FETCHER_KEY);
26            try {
27                $fetchers = ClassUtility::getObjectImplementingInterface(IFetcherPath::class);
28            } catch (\ReflectionException $e) {
29                throw new ExceptionInternal("We could read fetch classes via reflection Error: {$e->getMessage()}");
30            }
31            foreach ($fetchers as $fetcher) {
32                /**
33                 * @var IFetcherPath $fetcher
34                 */
35                if ($fetcher->getFetcherName() === $fetcherName) {
36                    $fetcher->buildFromUrl($fetchUrl);
37                    return $fetcher;
38                }
39            }
40        } catch (ExceptionNotFound $e) {
41            // no fetcher property
42        }
43
44
45        try {
46            $fetchDoku = FetcherRawLocalPath::createLocalFromFetchUrl($fetchUrl);
47            $dokuPath = $fetchDoku->getSourcePath();
48        } catch (ExceptionBadArgument $e) {
49            throw new ExceptionNotFound("No fetcher could be matched to the url ($fetchUrl)");
50        }
51        try {
52            $mime = FileSystems::getMime($dokuPath);
53        } catch (ExceptionNotFound $e) {
54            throw new ExceptionNotFound("No fetcher could be created. The mime is unknown for the path ($dokuPath). Error: {$e->getMessage()}");
55        }
56        switch ($mime->toString()) {
57            case Mime::SVG:
58                return FetcherSvg::createSvgFromFetchUrl($fetchUrl);
59            default:
60                if ($mime->isImage()) {
61                    return FetcherRaster::createRasterFromFetchUrl($fetchUrl);
62                } else {
63                    return $fetchDoku;
64                }
65        }
66
67    }
68
69    /**
70     * @throws ExceptionInternal - if we can't reflect the class
71     * @throws ExceptionNotFound - if the fetcher is unknown
72     * @throws ExceptionBadArgument - if the fetcher is not set
73     */
74    public static function createFetcherStringFromUrl(Url $fetchUrl): IFetcherString
75    {
76
77        try {
78            $fetcherName = $fetchUrl->getQueryPropertyValue(IFetcher::FETCHER_KEY);
79        } catch (ExceptionNotFound $e) {
80            throw new ExceptionBadArgument("No fetcher name found");
81        }
82        try {
83            $fetchers = ClassUtility::getObjectImplementingInterface(IFetcherString::class);
84        } catch (\ReflectionException $e) {
85            throw new ExceptionInternal("We could read fetch classes via reflection Error: {$e->getMessage()}");
86        }
87        foreach ($fetchers as $fetcher) {
88            /**
89             * @var IFetcherString $fetcher
90             */
91            if ($fetcher->getFetcherName() === $fetcherName) {
92                $fetcher->buildFromUrl($fetchUrl);
93                return $fetcher;
94            }
95        }
96        throw new ExceptionNotFound("No fetcher found with the name ($fetcherName)");
97
98    }
99
100}
101