|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Piwik\Policy; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Piwik\Plugin\Manager; |
| 7 | +use Piwik\Settings\FieldConfig; |
| 8 | +use Piwik\Settings\Interfaces\MeasurableSettingInterface; |
| 9 | +use Piwik\Settings\Interfaces\SystemSettingInterface; |
| 10 | +use Piwik\Settings\Interfaces\Traits\Setters\MeasurableSetterTrait; |
| 11 | +use Piwik\Settings\Interfaces\Traits\Setters\SystemSetterTrait; |
| 12 | + |
| 13 | +/** |
| 14 | + * @implements SystemSettingInterface<bool> |
| 15 | + * @implements MeasurableSettingInterface<bool> |
| 16 | + */ |
| 17 | +abstract class CompliancePolicy implements SystemSettingInterface, MeasurableSettingInterface |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @use SystemSetterTrait<bool> |
| 21 | + */ |
| 22 | + use SystemSetterTrait; |
| 23 | + |
| 24 | + /** |
| 25 | + * @use MeasurableSetterTrait<bool> |
| 26 | + */ |
| 27 | + use MeasurableSetterTrait; |
| 28 | + |
| 29 | + abstract public static function getName(): string; |
| 30 | + abstract public static function getDescription(): string; |
| 31 | + abstract public static function getTitle(): string; |
| 32 | + |
| 33 | + /** |
| 34 | + * @return array<string> of plugin names that are required for this policy to function |
| 35 | + */ |
| 36 | + abstract protected static function getMinimumRequiredPlugins(): array; |
| 37 | + |
| 38 | + /** |
| 39 | + * @return array<string, string> |
| 40 | + */ |
| 41 | + public static function getDetails(): array |
| 42 | + { |
| 43 | + return [ |
| 44 | + 'id' => static::getName(), |
| 45 | + 'title' => static::getTitle(), |
| 46 | + 'description' => static::getDescription(), |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @throws \Exception when required plugins are not active |
| 52 | + */ |
| 53 | + protected static function checkRequiredPluginsActive(): void |
| 54 | + { |
| 55 | + $plugins = static::getMinimumRequiredPlugins(); |
| 56 | + $pluginManager = static::getPluginManagerInstance(); |
| 57 | + |
| 58 | + foreach ($plugins as $plugin) { |
| 59 | + if (!$pluginManager->isPluginActivated($plugin)) { |
| 60 | + throw new Exception("Plugin $plugin is not activated"); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + protected static function getPluginManagerInstance(): Manager |
| 66 | + { |
| 67 | + return Manager::getInstance(); |
| 68 | + } |
| 69 | + |
| 70 | + protected static function getSystemDefaultValue() |
| 71 | + { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + protected static function getSystemName(): string |
| 76 | + { |
| 77 | + return preg_replace('/\s+/', '', static::getName()) . '_policy_enabled'; |
| 78 | + } |
| 79 | + |
| 80 | + protected static function getSystemType(): string |
| 81 | + { |
| 82 | + return FieldConfig::TYPE_BOOL; |
| 83 | + } |
| 84 | + |
| 85 | + protected static function getMeasurableDefaultValue() |
| 86 | + { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + protected static function getMeasurableName(): string |
| 91 | + { |
| 92 | + return preg_replace('/\s+/', '', static::getName()) . '_policy_enabled'; |
| 93 | + } |
| 94 | + |
| 95 | + protected static function getMeasurableType(): string |
| 96 | + { |
| 97 | + return FieldConfig::TYPE_BOOL; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * If the policy is active at the instance level, |
| 102 | + * disabling the policy for a site will also disable it |
| 103 | + * for the instance. |
| 104 | + */ |
| 105 | + public static function setActiveStatus(?int $idSite, bool $isActive): void |
| 106 | + { |
| 107 | + if (isset($idSite)) { |
| 108 | + static::setMeasurableValue($idSite, $isActive); |
| 109 | + if (static::getSystemValue() && !$isActive) { |
| 110 | + static::setSystemValue($isActive); |
| 111 | + } |
| 112 | + return; |
| 113 | + } |
| 114 | + static::setSystemValue($isActive); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * If the policy is active at the instance level, then |
| 119 | + * this function will return true for all sites. |
| 120 | + */ |
| 121 | + public static function isActive(?int $idSite): bool |
| 122 | + { |
| 123 | + try { |
| 124 | + self::checkRequiredPluginsActive(); |
| 125 | + } catch (Exception $e) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + $instanceLevel = static::getSystemValue(); |
| 130 | + if (!$instanceLevel && isset($idSite)) { |
| 131 | + return static::getMeasurableValue($idSite); |
| 132 | + } |
| 133 | + return $instanceLevel; |
| 134 | + } |
| 135 | +} |
0 commit comments