| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- /* * ********************************************************************
- * ProxmoxVPS product developed. (2019-09-06)
- * *
- *
- * CREATED BY MODULESGARDEN -> http://modulesgarden.com
- * CONTACT -> contact@modulesgarden.com
- *
- *
- * This software is furnished under a license and may be used and copied
- * only in accordance with the terms of such license and with the
- * inclusion of the above copyright notice. This software or any other
- * copies thereof may not be provided or otherwise made available to any
- * other person. No title to and ownership of the software is hereby
- * transferred.
- *
- *
- * ******************************************************************** */
- namespace ModulesGarden\ProxmoxAddon\App\Repositories;
- use ModulesGarden\ProxmoxAddon\App\Models\ServerConfiguration;
- use ModulesGarden\ProxmoxAddon\Core\Helper\WhmcsVersionComparator;
- /**
- * Description of ServerConfigurationRepository
- *
- * @author Pawel Kopec <pawelk@modulesgarden.com>
- * @version 1.0.1
- */
- abstract class AbstractServerConfigurationRepository
- {
- protected $serverId;
- /**
- *
- * @var ServerConfiguration[]
- */
- protected $entries;
- protected $force = false;
- public function __construct($serverId)
- {
- if (!is_numeric($serverId) || $serverId <= 0)
- {
- throw new \InvalidArgumentException(sprintf("Server id: %s is invalid", $serverId));
- }
- $this->serverId = $serverId;
- }
- /**
- * @return int|string
- */
- public function getServerId()
- {
- return $this->serverId;
- }
- /**
- * @param int|string $serverId
- */
- public function setServerId($serverId)
- {
- $this->serverId = $serverId;
- }
- public function getForce()
- {
- return $this->force;
- }
- public function setForce($force)
- {
- $this->force = $force;
- return $this;
- }
- public function isEmpty()
- {
- return empty($this->entries);
- }
- public function flush()
- {
- ServerConfiguration::ofServerId($this->serverId)
- ->delete();
- return $this;
- }
- /**
- * Delete keys
- * @param string $keys
- * @param array $keys
- * @return $this
- */
- public function forget($keys)
- {
- if (is_array($keys))
- {
- ServerConfiguration::ofServerId($this->serverId)
- ->whereIn('setting', $keys)
- ->delete();
- foreach ($keys as $k)
- {
- unset($this->entries[$k]);
- }
- }
- else
- {
- ServerConfiguration::where('server_id', $this->serverId)
- ->where('setting', $keys)
- ->limit(1)
- ->delete();
- unset($this->entries[$keys]);
- }
- return $this;
- }
- public function all($default = [])
- {
- $this->force = true;
- $decode = !WhmcsVersionComparator::isWhmcsVersionHigherOrEqual('8.0.0');
- foreach (ServerConfiguration::ofServerId($this->serverId)->pluck('value', 'setting')->all() as $key => $value)
- {
- if($decode){
- $value = json_decode($value, true);
- }
- $this->entries[$key] = $value;
- }
- if (!$this->isEmpty())
- {
- return $this->entries;
- }
- return $default;
- }
- public function store(array $values)
- {
- foreach ($values as $k => $v)
- {
- $this->set($k, $v);
- }
- return $this;
- }
- public function exist($key)
- {
- return ServerConfiguration::ofServerId($this->serverId)
- ->where('setting', $key)
- ->count() > 0;
- }
- public function set($key, $value)
- {
- $this->entries[$key] = $value;
- }
- public function __isset($key)
- {
- return isset($this->entries[$key]);
- }
- public function get($key, $default = null)
- {
- if (isset($this->entries[$key]))
- {
- return $this->entries[$key];
- }
- else
- {
- if (!$this->force)
- {
- $this->getEntery($key);
- if (isset($this->entries[$key]))
- {
- return $this->entries[$key];
- }
- }
- }
- return $default;
- }
- /**
- *
- * @param string $key
- * @return ServerConfiguration
- */
- protected function getEntery($key)
- {
- if (isset($this->entries[$key]))
- {
- return $this->entries[$key];
- }
- else
- {
- if (!$this->force)
- {
- $this->entries[$key] = ServerConfiguration::ofServerId($this->serverId)
- ->ofSetting($key)
- ->value("value");
- return $this->entries[$key];
- }
- }
- }
- public function fill(array $setting)
- {
- $this->entries = $setting;
- return $this;
- }
- public function fillAndSave(array $setting)
- {
- $this->entries = $setting;
- foreach ($this->entries as $key => $value)
- {
- $setting = ServerConfiguration::firstOrNew([
- 'server_id' => $this->serverId,
- 'setting' => $key
- ]);
- $setting->value = $value;
- $setting->server_id = $this->serverId;
- $setting->save();
- }
- return $this;
- }
- public function save()
- {
- foreach ($this->entries as $key => $value)
- {
- $setting = new ServerConfiguration();
- $setting->server_id = $this->serverId;
- $setting->setting = $key;
- $setting->value = $value;
- $setting->save();
- }
- }
- }
|