|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | + |
| 6 | +/** |
| 7 | + * Nextcloud - Backup |
| 8 | + * |
| 9 | + * This file is licensed under the Affero General Public License version 3 or |
| 10 | + * later. See the COPYING file. |
| 11 | + * |
| 12 | + * @author Maxence Lange <maxence@artificial-owl.com> |
| 13 | + * @copyright 2021, Maxence Lange <maxence@artificial-owl.com> |
| 14 | + * @license GNU AGPL version 3 or any later version |
| 15 | + * |
| 16 | + * This program is free software: you can redistribute it and/or modify |
| 17 | + * it under the terms of the GNU Affero General Public License as |
| 18 | + * published by the Free Software Foundation, either version 3 of the |
| 19 | + * License, or (at your option) any later version. |
| 20 | + * |
| 21 | + * This program is distributed in the hope that it will be useful, |
| 22 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | + * GNU Affero General Public License for more details. |
| 25 | + * |
| 26 | + * You should have received a copy of the GNU Affero General Public License |
| 27 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | + * |
| 29 | + */ |
| 30 | + |
| 31 | + |
| 32 | +namespace OCA\Backup\Command; |
| 33 | + |
| 34 | + |
| 35 | +use ArtificialOwl\MySmallPhpTools\Exceptions\InvalidItemException; |
| 36 | +use ArtificialOwl\MySmallPhpTools\Model\Request; |
| 37 | +use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc23\TNC23Deserialize; |
| 38 | +use OC\Core\Command\Base; |
| 39 | +use OCA\Backup\Db\RemoteRequest; |
| 40 | +use OCA\Backup\Exceptions\RemoteInstanceException; |
| 41 | +use OCA\Backup\Exceptions\RemoteInstanceNotFoundException; |
| 42 | +use OCA\Backup\Exceptions\RemoteResourceNotFoundException; |
| 43 | +use OCA\Backup\Model\RemoteInstance; |
| 44 | +use OCA\Backup\Model\RestoringPoint; |
| 45 | +use OCA\Backup\Service\RemoteStreamService; |
| 46 | +use Symfony\Component\Console\Input\InputInterface; |
| 47 | +use Symfony\Component\Console\Output\OutputInterface; |
| 48 | +use Symfony\Component\Console\Question\ChoiceQuestion; |
| 49 | + |
| 50 | + |
| 51 | +/** |
| 52 | + * Class RemoteList |
| 53 | + * |
| 54 | + * @package OCA\Backup\Command |
| 55 | + */ |
| 56 | +class RestoringBrowse extends Base { |
| 57 | + |
| 58 | + |
| 59 | + use TNC23Deserialize; |
| 60 | + |
| 61 | + |
| 62 | + const LOCAL = 'local'; |
| 63 | + |
| 64 | + |
| 65 | + /** @var RemoteRequest */ |
| 66 | + private $remoteRequest; |
| 67 | + |
| 68 | + /** @var RemoteStreamService */ |
| 69 | + private $remoteStreamService; |
| 70 | + |
| 71 | + |
| 72 | + public function __construct(RemoteRequest $remoteRequest, RemoteStreamService $remoteStreamService) { |
| 73 | + $this->remoteRequest = $remoteRequest; |
| 74 | + $this->remoteStreamService = $remoteStreamService; |
| 75 | + |
| 76 | + parent::__construct(); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * |
| 82 | + */ |
| 83 | + protected function configure() { |
| 84 | + $this->setName('backup:point:browse') |
| 85 | + ->setDescription('Browse restoring point'); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + /** |
| 90 | + * @param InputInterface $input |
| 91 | + * @param OutputInterface $output |
| 92 | + * |
| 93 | + * @return int |
| 94 | + * @throws RemoteInstanceException |
| 95 | + * @throws RemoteInstanceNotFoundException |
| 96 | + * @throws RemoteResourceNotFoundException |
| 97 | + */ |
| 98 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 99 | + $instance = $this->selectInstanceToBrowse($input, $output); |
| 100 | + |
| 101 | + $output->writeln(''); |
| 102 | + $output->writeln('Browsing available Restoring Point on <info>' . $instance . '</info>'); |
| 103 | + |
| 104 | + $rp = $this->getRestoringPoint($instance, ($instance === self::LOCAL)); |
| 105 | + echo 'RP: ' . json_encode($rp, JSON_PRETTY_PRINT); |
| 106 | + |
| 107 | + return 0; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + /** |
| 112 | + * @param InputInterface $input |
| 113 | + * @param OutputInterface $output |
| 114 | + * |
| 115 | + * @return string |
| 116 | + */ |
| 117 | + private function selectInstanceToBrowse(InputInterface $input, OutputInterface $output): string { |
| 118 | + $remote = array_filter( |
| 119 | + array_map( |
| 120 | + function (RemoteInstance $remoteInstance): ?string { |
| 121 | + if (!$remoteInstance->isOutgoing()) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + return $remoteInstance->getInstance(); |
| 126 | + }, $this->remoteRequest->getAll() |
| 127 | + ) |
| 128 | + ); |
| 129 | + |
| 130 | + $output->writeln(''); |
| 131 | + $helper = $this->getHelper('question'); |
| 132 | + $question = new ChoiceQuestion( |
| 133 | + 'Which location to browse ?', |
| 134 | + array_merge([self::LOCAL], $remote), |
| 135 | + 0 |
| 136 | + ); |
| 137 | + $question->setErrorMessage('Instance %s is not known.'); |
| 138 | + |
| 139 | + return $helper->ask($input, $output, $question); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + /** |
| 144 | + * @param string $instance |
| 145 | + * @param bool $local |
| 146 | + * |
| 147 | + * @return RestoringPoint[] |
| 148 | + * @throws RemoteInstanceNotFoundException |
| 149 | + * @throws RemoteInstanceException |
| 150 | + * @throws RemoteResourceNotFoundException |
| 151 | + * @throws InvalidItemException |
| 152 | + */ |
| 153 | + private function getRestoringPoint(string $instance, bool $local = false): array { |
| 154 | + if ($local) { |
| 155 | + return []; |
| 156 | + } |
| 157 | + |
| 158 | + $result = $this->remoteStreamService->resultRequestRemoteInstance( |
| 159 | + $instance, |
| 160 | + RemoteInstance::RP_LIST, |
| 161 | + Request::TYPE_GET |
| 162 | + ); |
| 163 | + |
| 164 | + return $this->deserializeArray($result, RestoringPoint::class); |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments