Odyssey
FeatureGateConfig.i
1 <?php
2 /**
3  * Class to retrieve and contain the database configuration for Feature Gates
4  *
5  */
6 
7 
9  protected static $instance;
10  protected $data = [];
11  protected $dbh;
12 
13  /**
14  * FeatureGateConfig constructor.
15  *
16  * @param $dbh Object Database handler
17  * @throws exception
18  */
19  protected function __construct($dbh) {
20  $this->dbh = $dbh;
21  $this->LoadConfig();
22  }
23 
24  /**
25  * Database access. Ideally this would be repository class.
26  *
27  * @return array Database result; empty array if no rows
28  * @throws exception
29  */
30  protected function GetConfig():array {
31  $sql = <<< SQL
32 SELECT feature, enabled_all, enabled_cu, params
33 FROM cufeaturegate;
34 SQL;
35  $query = db_query($sql, $this->dbh);
36  if (!$query) {
37  throw new Exception('Could not read cufeaturegate table.');
38  }
39  $rows = db_fetch_all($query);
40  if (!$rows) {
41  return [];
42  }
43  return $rows;
44  }
45 
46  /**
47  * Iterates through data, loading it by feature id
48  *
49  * @throws exception
50  */
51  protected function LoadConfig() {
52  $data = $this->GetConfig();
53  foreach ($data as $dataRow) {
54  $this->data[$dataRow['feature']] = [
55  'enabled_all' => $dataRow['enabled_all'],
56  'enabled_cu' => json_decode($dataRow['enabled_cu'], true) ?? [],
57  'params' => json_decode($dataRow['params'], true) ?? [],
58  ];
59  }
60  }
61 
62  /**
63  * Create or recover an instance of FeatureGateConfig
64  *
65  * @param $dbh Object Database handle
66  * @return FeatureGateConfig
67  * @throws exception
68  */
69  public static function GetInstance($dbh):FeatureGateConfig {
70  if (!static::$instance instanceof FeatureGateConfig) {
71  static::$instance = new FeatureGateConfig($dbh);
72  }
73  return static::$instance;
74  }
75 
76  /**
77  * Checks if the feature is represented in the config
78  *
79  * @param String $feature Indicates which feature to gate
80  * @return bool
81  */
82  public function HasFeature(String $feature):bool {
83  return array_key_exists($feature, $this->data);
84  }
85 
86  /**
87  * Return the config for a particular feature.
88  *
89  * @param String $feature Indicates which feature to gate
90  * @return bool|mixed
91  */
92  public function GetFeatureData(String $feature) {
93  if ($this->HasFeature($feature)) {
94  return $this->data[$feature];
95  }
96  return false;
97  }
98 }
static GetInstance($dbh)
HasFeature(String $feature)
GetFeatureData(String $feature)