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 return Json::createFromPath($analyticsCache)->toArray()[renderer_plugin_combo_analytics::QUALITY][renderer_plugin_combo_analytics::LOW]; 62 } catch (ExceptionCompile $e) { 63 $message = "Error while reading the json analytics. {$e->getMessage()}"; 64 LogUtility::internalError($message, self::CANONICAL); 65 throw new ExceptionNotFound($message); 66 } 67 68 } 69 70 } 71 72 73 static public function getLabel(): string 74 { 75 return "Low Quality Indicator"; 76 } 77 78 static public function getName(): string 79 { 80 return self::PROPERTY_NAME; 81 } 82 83 static public function getPersistenceType(): string 84 { 85 return Metadata::DERIVED_METADATA; 86 } 87 88 static public function isMutable(): bool 89 { 90 return false; 91 } 92 93 /** 94 * By default, if a file has not been through 95 * a {@link \renderer_plugin_combo_analytics} 96 * analysis, this is a low page if protection is set 97 */ 98 public function getDefaultValue(): bool 99 { 100 101 if (!Site::isLowQualityProtectionEnable()) { 102 return false; 103 } 104 return true; 105 } 106} 107