.htaccess
11 months ago
JetBackupLinux.php
11 months ago
JetBackupLinuxObject.php
11 months ago
Query.php
11 months ago
QueueItem.php
11 months ago
index.html
11 months ago
web.config
11 months ago
JetBackupLinuxObject.php
102 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\JetBackupLinux; |
| 4 | |
| 5 | use JetBackup\Data\ArrayData; |
| 6 | use JetBackup\Exception\JetBackupLinuxException; |
| 7 | use JetBackup\SocketAPI\Exception\SocketAPIException; |
| 8 | use JetBackup\SocketAPI\SocketAPI; |
| 9 | |
| 10 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 11 | |
| 12 | class JetBackupLinuxObject extends ArrayData { |
| 13 | |
| 14 | const ID_FIELD = '_id'; |
| 15 | |
| 16 | private string $_item; |
| 17 | private array $_data=[]; |
| 18 | |
| 19 | public function __construct($item) { |
| 20 | $this->_item = $item; |
| 21 | } |
| 22 | |
| 23 | public function set($key, $value):void { |
| 24 | parent::set($key, $value); |
| 25 | $this->_data[$key] = $value; |
| 26 | } |
| 27 | |
| 28 | public function setData($data=[]):void { |
| 29 | parent::setData($data); |
| 30 | $this->_data = []; |
| 31 | } |
| 32 | |
| 33 | public function setId(string $id):void { $this->set(self::ID_FIELD, $id); } |
| 34 | public function getId():string { return $this->get(self::ID_FIELD); } |
| 35 | |
| 36 | /** |
| 37 | * @param string $id |
| 38 | * |
| 39 | * @return void |
| 40 | * @throws JetBackupLinuxException |
| 41 | * @throws SocketAPIException |
| 42 | */ |
| 43 | protected function load(string $id):void { |
| 44 | if(!JetBackupLinux::isEnabled()) return; |
| 45 | |
| 46 | $query = SocketAPI::api( 'get' . $this->_item); |
| 47 | $query->arg(self::ID_FIELD, $id); |
| 48 | |
| 49 | try { |
| 50 | $response = $query->execute(); |
| 51 | } catch(SocketAPIException $e) { |
| 52 | throw new JetBackupLinuxException($e->getMessage()); |
| 53 | } |
| 54 | |
| 55 | if(!$response['success']) throw new JetBackupLinuxException($response['message']); |
| 56 | $this->setData($response['data']); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @return void |
| 61 | * @throws JetBackupLinuxException |
| 62 | * @throws SocketAPIException |
| 63 | */ |
| 64 | public function save():void { |
| 65 | if(!JetBackupLinux::isEnabled()) return; |
| 66 | |
| 67 | $query = SocketAPI::api( 'manage' . $this->_item); |
| 68 | if($this->getId()) $query->arg(self::ID_FIELD, $this->getId()); |
| 69 | $query->arg('action', $this->getId() ? 'modify' : 'create'); |
| 70 | |
| 71 | foreach($this->_data as $key => $value) $query->arg($key, $value); |
| 72 | |
| 73 | try { |
| 74 | $response = $query->execute(); |
| 75 | } catch(SocketAPIException $e) { |
| 76 | throw new JetBackupLinuxException($e->getMessage()); |
| 77 | } |
| 78 | |
| 79 | if(!$response['success']) throw new JetBackupLinuxException($response['message']); |
| 80 | $this->setData($response['data']); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @return void |
| 85 | * @throws JetBackupLinuxException |
| 86 | * @throws SocketAPIException |
| 87 | */ |
| 88 | public function delete():void { |
| 89 | if(!JetBackupLinux::isEnabled() || !$this->getId()) return; |
| 90 | |
| 91 | $query = SocketAPI::api( 'delete' . $this->_item); |
| 92 | $query->arg(self::ID_FIELD, $this->getId()); |
| 93 | |
| 94 | try { |
| 95 | $query->execute(); |
| 96 | } catch(SocketAPIException $e) { |
| 97 | throw new JetBackupLinuxException($e->getMessage()); |
| 98 | } |
| 99 | |
| 100 | $this->setData(); |
| 101 | } |
| 102 | } |