Passed
Push — master ( c4d71c...15ef35 )
by Morris
13:37 queued 11s
created

VersionManager::rollback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Files_Versions\Versions;
23
24
use OCP\Files\File;
25
use OCP\Files\FileInfo;
26
use OCP\Files\Storage\IStorage;
27
use OCP\IUser;
28
29
class VersionManager implements IVersionManager {
30
	/** @var IVersionBackend[] */
31
	private $backends = [];
32
33
	public function registerBackend(string $storageType, IVersionBackend $backend) {
34
		$this->backends[$storageType] = $backend;
35
	}
36
37
	/**
38
	 * @return IVersionBackend[]
39
	 */
40
	private function getBackends(): array {
41
		return $this->backends;
42
	}
43
44
	/**
45
	 * @param IStorage $storage
46
	 * @return IVersionBackend
47
	 * @throws BackendNotFoundException
48
	 */
49
	public function getBackendForStorage(IStorage $storage): IVersionBackend {
50
		$fullType = get_class($storage);
51
		$backends = $this->getBackends();
52
		$foundType = array_reduce(array_keys($backends), function ($type, $registeredType) use ($storage) {
53
			if (
54
				$storage->instanceOfStorage($registeredType) &&
55
				($type === '' || is_subclass_of($registeredType, $type))
56
			) {
57
				return $registeredType;
58
			} else {
59
				return $type;
60
			}
61
		}, '');
62
		if ($foundType === '') {
63
			throw new BackendNotFoundException("Version backend for $fullType not found");
64
		} else {
65
			return $backends[$foundType];
66
		}
67
	}
68
69
	public function getVersionsForFile(IUser $user, FileInfo $file): array {
70
		$backend = $this->getBackendForStorage($file->getStorage());
71
		return $backend->getVersionsForFile($user, $file);
72
	}
73
74
	public function createVersion(IUser $user, FileInfo $file) {
75
		$backend = $this->getBackendForStorage($file->getStorage());
76
		$backend->createVersion($user, $file);
77
	}
78
79
	public function rollback(IVersion $version) {
80
		$backend = $version->getBackend();
81
		return $backend->rollback($version);
82
	}
83
84
	public function read(IVersion $version) {
85
		$backend = $version->getBackend();
86
		return $backend->read($version);
87
	}
88
89
	public function getVersionFile(IUser $user, FileInfo $sourceFile, int $revision): File {
90
		$backend = $this->getBackendForStorage($sourceFile->getStorage());
91
		return $backend->getVersionFile($user, $sourceFile, $revision);
92
	}
93
}
94