Odyssey
FeatureGate.i
1 <?php
2 /**
3  * Abstract base class to determine if a gate is passed. A gate can look at the
4  * credit union and if that passes look to see if there is an additional username
5  * filter. The minimal implementation is for a credit union to pass. Other entry
6  * points will allow testing of other parameters to make sure those requirements
7  * pass the gate check.
8  *
9  */
10 
11 abstract class FeatureGate {
12  protected $featureGateConfig;
13  protected $feature = '';
14 
15  /**
16  * FeatureGate constructor.
17  *
18  * @param String $feature Indicates which feature to gate
19  * @param FeatureGateConfig $config
20  * @throws Exception
21  */
22  public function __construct(String $feature, FeatureGateConfig $config) {
23  $this->featureGateConfig = $config;
24  if (strlen(trim($feature)) == 0) {
25  throw new Exception('Must supply a non-empty feature');
26  }
27  $this->feature = $feature;
28  }
29 
30  /**
31  * Check if credit union is allowed to pass.
32  *
33  * @param String $creditUnion Credit union to check the gate for
34  * @param array $params Extra params for the gate check [Optional]
35  * @return bool
36  */
37  public function WillPass(String $creditUnion, array $params = null):bool {
38  return $this->DeterminePass($creditUnion, $params);
39  }
40 
41  /**
42  * Since all possible types of feature gates must start with a check for the credit union, this
43  * is located in the abstract class. If the feature does not exist, default return to false.
44  *
45  * @param String $creditUnion Credit union to check the gate for
46  * @return bool
47  */
48  protected function DetermineCreditUnionPass(String $creditUnion):bool {
49  $config = $this->featureGateConfig->GetFeatureData($this->feature);
50  if ($config) {
51  switch ($config['enabled_all']) {
52  case 'Y': // YES
53  return true;
54  break;
55  case 'O': // OFF
56  return false;
57  break;
58  case 'N': // NOT EVERONE - check credit union
59  return in_array($creditUnion, $config['enabled_cu']);
60  break;
61  default:
62  return false;
63  }
64  }
65  return false;
66  }
67 
68  /**
69  * Return the params for a particular feature. This should only be called if
70  * the caller knows the credit union is allowed (i.e DetermineCreditUnionPass returned true).
71  *
72  * params would be json string object in the form:
73  * {"creditUnion":{"usernames":["BongoBob","958777"],"ip":["192.168.1.14"],"someOtherParam":["item1","item2"]}}
74  * example: {"MYCU":{"usernames":["smith","jones"]}, "SCRUBCU":{"usernames":["bongobob", "4188"]}}
75  *
76  * @param String $feature Indicates which feature to gate
77  * @return bool|array
78  */
79  protected function GetFeatureParams(String $creditUnion) {
80  if ($this->featureGateConfig->HasFeature($this->feature)) {
81  $config = $this->featureGateConfig->GetFeatureData($this->feature);
82 
83  if ( $config && is_array($config) && array_key_exists("params", $config) ) {
84  // convert json params to array
85  $paramsByCU = $config["params"];
86  $returnArray = isset( $paramsByCU[$creditUnion] ) ? $paramsByCU[$creditUnion] : array();
87 
88  return $returnArray;
89  }
90  }
91  return false;
92  }
93 
94  /**
95  * Abstract method to determine if the credit union does, indeed, pass
96  *
97  * @param String $creditUnion Credit union to check the gate for
98  * @param array $params Extra params for the gate check [Optional]
99  * @return bool
100  */
101  abstract protected function DeterminePass(String $creditUnion, array $params = null):bool;
102 
103 }
WillPass(String $creditUnion, array $params=null)
Definition: FeatureGate.i:37
__construct(String $feature, FeatureGateConfig $config)
Definition: FeatureGate.i:22
DeterminePass(String $creditUnion, array $params=null)
GetFeatureParams(String $creditUnion)
Definition: FeatureGate.i:79
DetermineCreditUnionPass(String $creditUnion)
Definition: FeatureGate.i:48