AbstractServerConfigurationRepository.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2019-09-06)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\ProxmoxAddon\App\Repositories;
  20. use ModulesGarden\ProxmoxAddon\App\Models\ServerConfiguration;
  21. use ModulesGarden\ProxmoxAddon\Core\Helper\WhmcsVersionComparator;
  22. /**
  23. * Description of ServerConfigurationRepository
  24. *
  25. * @author Pawel Kopec <pawelk@modulesgarden.com>
  26. * @version 1.0.1
  27. */
  28. abstract class AbstractServerConfigurationRepository
  29. {
  30. protected $serverId;
  31. /**
  32. *
  33. * @var ServerConfiguration[]
  34. */
  35. protected $entries;
  36. protected $force = false;
  37. public function __construct($serverId)
  38. {
  39. if (!is_numeric($serverId) || $serverId <= 0)
  40. {
  41. throw new \InvalidArgumentException(sprintf("Server id: %s is invalid", $serverId));
  42. }
  43. $this->serverId = $serverId;
  44. }
  45. /**
  46. * @return int|string
  47. */
  48. public function getServerId()
  49. {
  50. return $this->serverId;
  51. }
  52. /**
  53. * @param int|string $serverId
  54. */
  55. public function setServerId($serverId)
  56. {
  57. $this->serverId = $serverId;
  58. }
  59. public function getForce()
  60. {
  61. return $this->force;
  62. }
  63. public function setForce($force)
  64. {
  65. $this->force = $force;
  66. return $this;
  67. }
  68. public function isEmpty()
  69. {
  70. return empty($this->entries);
  71. }
  72. public function flush()
  73. {
  74. ServerConfiguration::ofServerId($this->serverId)
  75. ->delete();
  76. return $this;
  77. }
  78. /**
  79. * Delete keys
  80. * @param string $keys
  81. * @param array $keys
  82. * @return $this
  83. */
  84. public function forget($keys)
  85. {
  86. if (is_array($keys))
  87. {
  88. ServerConfiguration::ofServerId($this->serverId)
  89. ->whereIn('setting', $keys)
  90. ->delete();
  91. foreach ($keys as $k)
  92. {
  93. unset($this->entries[$k]);
  94. }
  95. }
  96. else
  97. {
  98. ServerConfiguration::where('server_id', $this->serverId)
  99. ->where('setting', $keys)
  100. ->limit(1)
  101. ->delete();
  102. unset($this->entries[$keys]);
  103. }
  104. return $this;
  105. }
  106. public function all($default = [])
  107. {
  108. $this->force = true;
  109. $decode = !WhmcsVersionComparator::isWhmcsVersionHigherOrEqual('8.0.0');
  110. foreach (ServerConfiguration::ofServerId($this->serverId)->pluck('value', 'setting')->all() as $key => $value)
  111. {
  112. if($decode){
  113. $value = json_decode($value, true);
  114. }
  115. $this->entries[$key] = $value;
  116. }
  117. if (!$this->isEmpty())
  118. {
  119. return $this->entries;
  120. }
  121. return $default;
  122. }
  123. public function store(array $values)
  124. {
  125. foreach ($values as $k => $v)
  126. {
  127. $this->set($k, $v);
  128. }
  129. return $this;
  130. }
  131. public function exist($key)
  132. {
  133. return ServerConfiguration::ofServerId($this->serverId)
  134. ->where('setting', $key)
  135. ->count() > 0;
  136. }
  137. public function set($key, $value)
  138. {
  139. $this->entries[$key] = $value;
  140. }
  141. public function __isset($key)
  142. {
  143. return isset($this->entries[$key]);
  144. }
  145. public function get($key, $default = null)
  146. {
  147. if (isset($this->entries[$key]))
  148. {
  149. return $this->entries[$key];
  150. }
  151. else
  152. {
  153. if (!$this->force)
  154. {
  155. $this->getEntery($key);
  156. if (isset($this->entries[$key]))
  157. {
  158. return $this->entries[$key];
  159. }
  160. }
  161. }
  162. return $default;
  163. }
  164. /**
  165. *
  166. * @param string $key
  167. * @return ServerConfiguration
  168. */
  169. protected function getEntery($key)
  170. {
  171. if (isset($this->entries[$key]))
  172. {
  173. return $this->entries[$key];
  174. }
  175. else
  176. {
  177. if (!$this->force)
  178. {
  179. $this->entries[$key] = ServerConfiguration::ofServerId($this->serverId)
  180. ->ofSetting($key)
  181. ->value("value");
  182. return $this->entries[$key];
  183. }
  184. }
  185. }
  186. public function fill(array $setting)
  187. {
  188. $this->entries = $setting;
  189. return $this;
  190. }
  191. public function fillAndSave(array $setting)
  192. {
  193. $this->entries = $setting;
  194. foreach ($this->entries as $key => $value)
  195. {
  196. $setting = ServerConfiguration::firstOrNew([
  197. 'server_id' => $this->serverId,
  198. 'setting' => $key
  199. ]);
  200. $setting->value = $value;
  201. $setting->server_id = $this->serverId;
  202. $setting->save();
  203. }
  204. return $this;
  205. }
  206. public function save()
  207. {
  208. foreach ($this->entries as $key => $value)
  209. {
  210. $setting = new ServerConfiguration();
  211. $setting->server_id = $this->serverId;
  212. $setting->setting = $key;
  213. $setting->value = $value;
  214. $setting->save();
  215. }
  216. }
  217. }