AbstractProductConfigurationRepository.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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\Core\Helper\WhmcsVersionComparator;
  21. use ModulesGarden\ProxmoxAddon\App\Models\ProductConfiguration;
  22. /**
  23. * Description of ProductConfigurationRepository
  24. *
  25. * @author Pawel Kopec <pawelk@modulesgarden.com>
  26. * @version 1.0.1
  27. */
  28. abstract class AbstractProductConfigurationRepository
  29. {
  30. protected $productId;
  31. /**
  32. *
  33. * @var ProductConfiguration[]
  34. */
  35. protected $entries;
  36. protected $force = false;
  37. public function __construct($productId)
  38. {
  39. if (!is_numeric($productId) || $productId <= 0)
  40. {
  41. throw new \InvalidArgumentException(sprintf("Product id: %s is invalid", $productId));
  42. }
  43. $this->productId = $productId;
  44. }
  45. /**
  46. * @return int|string
  47. */
  48. public function getProductId()
  49. {
  50. return $this->productId;
  51. }
  52. /**
  53. * @param int|string $productId
  54. */
  55. public function setProductId($productId)
  56. {
  57. $this->productId = $productId;
  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. ProductConfiguration::ofProductId($this->productId)
  75. ->whereNotIn('setting', ['version'])
  76. ->delete();
  77. return $this;
  78. }
  79. /**
  80. * Delete keys
  81. * @param string $keys
  82. * @param array $keys
  83. * @return $this
  84. */
  85. public function forget($keys)
  86. {
  87. if (is_array($keys))
  88. {
  89. ProductConfiguration::ofProductId($this->productId)
  90. ->whereIn('setting', $keys)
  91. ->delete();
  92. foreach ($keys as $k)
  93. {
  94. unset($this->entries[$k]);
  95. }
  96. }
  97. else
  98. {
  99. ProductConfiguration::where('product_id', $this->productId)
  100. ->where('setting', $keys)
  101. ->limit(1)
  102. ->delete();
  103. unset($this->entries[$keys]);
  104. }
  105. return $this;
  106. }
  107. public function all($default = [])
  108. {
  109. $this->force = true;
  110. $decode = !WhmcsVersionComparator::isWhmcsVersionHigherOrEqual('8.0.0');
  111. foreach (ProductConfiguration::ofProductId($this->productId)->pluck('value', 'setting')->all() as $key => $value)
  112. {
  113. if($decode){
  114. $value = json_decode($value, true);
  115. }
  116. $this->entries[$key] = $value;
  117. }
  118. if (!$this->isEmpty())
  119. {
  120. return $this->entries;
  121. }
  122. return $default;
  123. }
  124. public function store(array $values)
  125. {
  126. foreach ($values as $k => $v)
  127. {
  128. $this->set($k, $v);
  129. }
  130. return $this;
  131. }
  132. public function exist($key)
  133. {
  134. return ProductConfiguration::ofProductId($this->productId)
  135. ->where('setting', $key)
  136. ->count() > 0;
  137. }
  138. public function set($key, $value)
  139. {
  140. $this->enterie[$key] = $value;
  141. }
  142. public function __isset($key)
  143. {
  144. return isset($this->entries[$key]);
  145. }
  146. public function get($key, $default = null)
  147. {
  148. if (isset($this->entries[$key]))
  149. {
  150. return $this->entries[$key];
  151. }
  152. else
  153. {
  154. if (!$this->force)
  155. {
  156. $this->getEntery($key);
  157. if (isset($this->entries[$key]))
  158. {
  159. return $this->entries[$key];
  160. }
  161. }
  162. }
  163. return $default;
  164. }
  165. /**
  166. *
  167. * @param string $key
  168. * @return ProductConfiguration
  169. */
  170. protected function getEntery($key)
  171. {
  172. if (isset($this->entries[$key]))
  173. {
  174. return $this->entries[$key];
  175. }
  176. else
  177. {
  178. if (!$this->force)
  179. {
  180. $this->entries[$key] = ProductConfiguration::ofProductId($this->productId)
  181. ->ofSetting($key)
  182. ->value("value");
  183. return $this->entries[$key];
  184. }
  185. }
  186. }
  187. public function fill(array $setting)
  188. {
  189. $this->entries = $setting;
  190. return $this;
  191. }
  192. public function fillAndSave(array $setting)
  193. {
  194. $this->entries = $setting;
  195. foreach ($this->entries as $key => $value)
  196. {
  197. $setting = ProductConfiguration::firstOrNew([
  198. 'product_id' => $this->productId,
  199. 'setting' => $key
  200. ]);
  201. $setting->value = $value;
  202. $setting->save();
  203. }
  204. return $this;
  205. }
  206. public function save()
  207. {
  208. foreach ($this->entries as $key => $value)
  209. {
  210. $setting = new ProductConfiguration();
  211. $setting->product_id = $this->productId;
  212. $setting->setting = $key;
  213. $setting->value = $value;
  214. $setting->save();
  215. }
  216. }
  217. public function getVersion()
  218. {
  219. return $this->get('version');
  220. }
  221. public function setVersion($version)
  222. {
  223. $this->set('version', $version);
  224. return $this;
  225. }
  226. }