1<?php
2
3namespace ComboStrap\Meta\Field;
4
5use ComboStrap\ExceptionNotFound;
6use ComboStrap\FileSystems;
7use ComboStrap\FirstRasterImage;
8use ComboStrap\MarkupPath;
9use ComboStrap\Meta\Api\Metadata;
10use ComboStrap\Meta\Api\MetadataImage;
11use ComboStrap\Meta\Store\MetadataDokuWikiStore;
12use ComboStrap\PageImageUsage;
13use ComboStrap\ResourceCombo;
14use ComboStrap\Site;
15use ComboStrap\WikiPath;
16
17/**
18 * A field that derived the featured image for social network
19 *
20 * (The featured image for a html page may be a svg
21 * while for a social network, it should not)
22 *
23 * This meta returns the first raster image found.
24 */
25class SocialCardImage extends MetadataImage
26{
27
28
29    const PROPERTY_NAME = "social-card-image";
30
31    public static function createFromResourcePage(MarkupPath $page): SocialCardImage
32    {
33        return (new SocialCardImage())->setResource($page);
34    }
35
36    static public function getDescription(): string
37    {
38        return "The image for social card";
39    }
40
41    static public function getLabel(): string
42    {
43        return "Social Card Image";
44    }
45
46    public static function getName(): string
47    {
48        return self::PROPERTY_NAME;
49    }
50
51    static public function getPersistenceType(): string
52    {
53        return Metadata::DERIVED_METADATA;
54    }
55
56
57    static public function isMutable(): bool
58    {
59        return false;
60    }
61
62    public function getValue(): WikiPath
63    {
64
65        $contextPage = $this->getResource();
66        $actual = $contextPage;
67        while (true) {
68            try {
69                return $this->getFeaturedRasterImageOrFirst($actual);
70            } catch (ExceptionNotFound $e) {
71                // ok, vignette ?
72            }
73            try {
74                $actual = $actual->getParent();
75            } catch (ExceptionNotFound $e) {
76                break;
77            }
78        }
79        try {
80            return Site::getLogoAsRasterImage()->getSourcePath();
81        } catch (ExceptionNotFound $e) {
82            return FeaturedRasterImage::getComboStrapLogo();
83        }
84
85    }
86
87    /**
88     * @throws ExceptionNotFound
89     */
90    private function getFeaturedRasterImageOrFirst(ResourceCombo $contextPage): WikiPath
91    {
92        $featuredRasterImage = FeaturedRasterImage::createFromResourcePage($contextPage);
93        try {
94            return $featuredRasterImage->getValueOrParsed();
95        } catch (ExceptionNotFound $e) {
96            return FirstRasterImage::createForPage($contextPage)->getValue();
97        }
98    }
99
100    static public function getDrive(): string
101    {
102        return WikiPath::MEDIA_DRIVE;
103    }
104
105}
106