DONE: feature flags by branches
This commit is contained in:
+147
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
class Feature
|
||||
{
|
||||
const PEMBIAYAAN_ONLINE = 'pembiayaan_online';
|
||||
|
||||
/**
|
||||
* Known flag keys. New features: add a constant here, then INSERT rows for pilot kodcaw.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public static function keys()
|
||||
{
|
||||
return [
|
||||
self::PEMBIAYAAN_ONLINE,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string,int>
|
||||
*/
|
||||
public static function defaults()
|
||||
{
|
||||
$map = [];
|
||||
foreach (self::keys() as $key) {
|
||||
$map[$key] = 0;
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display labels for admin UI. New features: add a label here.
|
||||
*
|
||||
* @return array<string,string>
|
||||
*/
|
||||
public static function labels()
|
||||
{
|
||||
return [
|
||||
self::PEMBIAYAAN_ONLINE => 'Pembiayaan Online',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Missing row is off (fail closed).
|
||||
*
|
||||
* @param string $kodcaw
|
||||
* @param string $feature
|
||||
* @return bool
|
||||
*/
|
||||
public static function enabled($kodcaw, $feature)
|
||||
{
|
||||
return CawanganFeature::on('mysql7')
|
||||
->where('kodcaw', $kodcaw)
|
||||
->where('feature', $feature)
|
||||
->where('enabled', 1)
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $kodcaw
|
||||
* @return array<string,int>
|
||||
*/
|
||||
public static function mapFor($kodcaw)
|
||||
{
|
||||
$map = self::defaults();
|
||||
|
||||
$rows = CawanganFeature::on('mysql7')
|
||||
->where('kodcaw', $kodcaw)
|
||||
->get(['feature', 'enabled']);
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$map[$row->feature] = ((int) $row->enabled) === 1 ? 1 : 0;
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $kodcaws
|
||||
* @return array<string,array<string,int>>
|
||||
*/
|
||||
public static function mapForMany(array $kodcaws)
|
||||
{
|
||||
$defaults = self::defaults();
|
||||
$result = [];
|
||||
foreach ($kodcaws as $kodcaw) {
|
||||
$result[$kodcaw] = $defaults;
|
||||
}
|
||||
|
||||
if (count($kodcaws) === 0) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$rows = CawanganFeature::on('mysql7')
|
||||
->whereIn('kodcaw', $kodcaws)
|
||||
->get(['kodcaw', 'feature', 'enabled']);
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$result[$row->kodcaw][$row->feature] = ((int) $row->enabled) === 1 ? 1 : 0;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $kodcaw
|
||||
* @param string $feature
|
||||
* @param bool|int $enabled
|
||||
* @return bool
|
||||
*/
|
||||
public static function setEnabled($kodcaw, $feature, $enabled)
|
||||
{
|
||||
if (!in_array($feature, self::keys(), true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
CawanganFeature::on('mysql7')->updateOrCreate(
|
||||
[
|
||||
'kodcaw' => $kodcaw,
|
||||
'feature' => $feature,
|
||||
],
|
||||
[
|
||||
'enabled' => $enabled ? 1 : 0,
|
||||
]
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $message
|
||||
* @return array{ok:bool,status:int,payload:array}
|
||||
*/
|
||||
public static function denied($message = null)
|
||||
{
|
||||
return [
|
||||
'ok' => false,
|
||||
'status' => 403,
|
||||
'payload' => [
|
||||
'message' => $message ?: 'Ciri ini tidak diaktifkan untuk cawangan ini',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user