1<?php
2
3
4namespace ComboStrap;
5
6
7use ComboStrap\Meta\Api\Metadata;
8use ComboStrap\Meta\Api\MetadataBoolean;
9use renderer_plugin_combo_analytics;
10
11class LowQualityCalculatedIndicator extends MetadataBoolean
12{
13
14    public const PROPERTY_NAME = "low_quality_indicator_calculated";
15
16    public static function createFromPage(MarkupPath $page)
17    {
18        return (new LowQualityCalculatedIndicator())
19            ->setResource($page);
20    }
21
22    public static function getTab(): ?string
23    {
24        // not in a form
25        return null;
26    }
27
28    public static function getDescription(): string
29    {
30        return "The indicator calculated by the analytics process that tells if a page is of a low quality";
31    }
32
33    public function getValue(): bool
34    {
35
36        try {
37            return parent::getValue();
38        } catch (ExceptionNotFound $e) {
39
40            /**
41             * Migration code
42             * The indicator {@link LowQualityCalculatedIndicator::PROPERTY_NAME} is new
43             * but if the analytics was done, we can get it
44             */
45            $resource = $this->getResource();
46            if (!($resource instanceof MarkupPath)) {
47                throw new ExceptionNotFound("Low Quality is only for page resources");
48            }
49            try {
50                $analyticsDocument = $resource->fetchAnalyticsDocument();
51            } catch (ExceptionNotExists $e) {
52                throw new ExceptionNotFound("No analytics document could be found");
53            }
54
55            $analyticsCache = $analyticsDocument->getContentCachePath();
56            if (!FileSystems::exists($analyticsCache)) {
57                throw new ExceptionNotFound("No analytics document could be found");
58            }
59
60            try {
61                $value = Json::createFromPath($analyticsCache)->toArray()[renderer_plugin_combo_analytics::QUALITY][renderer_plugin_combo_analytics::LOW];
62                if ($value === null) {
63                    throw new ExceptionNotFound("The value is null in the analytics document.");
64                }
65                return DataType::toBoolean($value, true);
66            } catch (ExceptionCompile $e) {
67                $message = "Error while reading the json analytics. {$e->getMessage()}";
68                LogUtility::internalError($message, self::CANONICAL);
69                throw new ExceptionNotFound($message);
70            }
71
72        }
73
74    }
75
76
77    static public function getLabel(): string
78    {
79        return "Low Quality Indicator";
80    }
81
82    static public function getName(): string
83    {
84        return self::PROPERTY_NAME;
85    }
86
87    static public function getPersistenceType(): string
88    {
89        return Metadata::DERIVED_METADATA;
90    }
91
92    static public function isMutable(): bool
93    {
94        return false;
95    }
96
97    /**
98     * By default, if a file has not been through
99     * a {@link \renderer_plugin_combo_analytics}
100     * analysis, this is a low page if protection is set
101     */
102    public function getDefaultValue(): bool
103    {
104
105        if (!Site::isLowQualityProtectionEnable()) {
106            return false;
107        }
108        return true;
109    }
110}
111