Advertisement
Guest User

Untitled

a guest
Apr 4th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. <?php
  2.  
  3. // could be abstract class?
  4. interface FeatureInterface {
  5. function isSupported();
  6. }
  7.  
  8. interface LetmeinInterface extends FeatureInterface {
  9. function letmein($username, $password);
  10. }
  11.  
  12. class CWPLetmein implements LetmeinInterface {
  13.  
  14. function _construct(\CWPEnvironment $environment) {
  15. $this->env = $environment;
  16. }
  17.  
  18. function isSupported() {
  19. // CWPLetmeInConfig is a DataObject with a HasOne to CWPEnvironment that contains data
  20. // that DNEnvironment shouldnt care about. It's a feature specific data stored somewhere ...
  21. $config = CWPLetmeInConfig::get()->where('CWPEnvironmentID', $this->env->ID);
  22. return $config->isEnabled;
  23. }
  24.  
  25. function letmein($username, $password) {
  26. $config = CWPLetmeInConfig::get()->where('CWPEnvironmentID', $this->env->ID);
  27. $config->username = $username;
  28. $config->password = $password;
  29. $config->write();
  30. $this->schedulePuppetRerun($config);
  31. return true;
  32. }
  33. }
  34.  
  35. class RainforestLetmein implements LetmeinInterface {
  36.  
  37. function _construct(\RainforestEnvironment $environment) {
  38. $this->env = $environment;
  39. }
  40.  
  41. function isSupported() {
  42. if (!$this->env->manifestAtLeat('4.1.3')) {
  43. throw new BackendUnsupportedException('at least 4.1.3');
  44. }
  45. return true;
  46. }
  47.  
  48. // Use $this->env to get base data, maybe this backend queries some other data?
  49. function letmein($username, $password) {
  50. return true;
  51. }
  52. }
  53.  
  54. // mysite specific environment
  55. class RainforestEnvironment {
  56.  
  57. function supports($feature) {
  58. if($this->features == null) {
  59. $this->features = $this->initSupports();
  60. }
  61.  
  62. if(isset($this->features[$feature])) {
  63. // this might throw an exception, or highlight that it's supported, but not usable at the moment
  64. if($this->features[$feature]->isSupported()) {
  65. $this->features[$feature];
  66. }
  67. }
  68. return false;
  69. }
  70.  
  71. // could be setup somewhere else
  72. function initSupports() {
  73. $features = [
  74. GenericFeatureInterface::class => new SomeGenericFeature($this),
  75. DomainsSupportInterface::class => new RainforestDomains($this),
  76. ];
  77. if($this->manifestAtLeast('4.1.3')) {
  78. $features[LetmeinInterface::class] => new RainforestLetmein($this),
  79. }
  80.  
  81. // db value, feature flag, bad example perhaps?
  82. if($this->NewDeployModal) {
  83. $features[DeployScreenInterface::class] = new DeployDispatcher($this);
  84. } else {
  85. $features[DeployScreenInterface::class] = new DNRoot($this); // eh, I hope you get the point
  86. }
  87. return $features;
  88. }
  89. }
  90.  
  91. // On UI dispatcher action:
  92. try {
  93. if ($feature = $env->supports(LetmeinInterface::class)) {
  94. $feature->letmein($username, $password);
  95. }
  96. } catch (\BackendUnsupportedException $ex) {
  97. // show warning for user? can we use something else than exceptions?
  98. }
  99.  
  100. // UI / Rendering? where is that? I dunno..
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement