Passed
Push — master ( 9daee1...553543 )
by Morris
11:21
created

VersionManager::createVersion()   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 2
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
		if (!isset($this->backends[$storageType])) {
35
			$this->backends[$storageType] = [];
36
		}
37
		$this->backends[$storageType][] = $backend;
38
	}
39
40
	/**
41
	 * @return (IVersionBackend[])[]
42
	 */
43
	private function getBackends(): array {
44
		return $this->backends;
45
	}
46
47
	/**
48
	 * @param IStorage $storage
49
	 * @return IVersionBackend
50
	 * @throws BackendNotFoundException
51
	 */
52
	public function getBackendForStorage(IStorage $storage): IVersionBackend {
53
		$fullType = get_class($storage);
54
		$backends = $this->getBackends();
55
56
		$foundType = '';
57
		$foundBackend = null;
58
59
		foreach ($backends as $type => $backendsForType) {
60
			if (
61
				$storage->instanceOfStorage($type) &&
62
				($foundType === '' || is_subclass_of($type, $foundType))
63
			) {
64
				foreach ($backendsForType as $backend) {
65
					/** @var IVersionBackend $backend */
66
					if ($backend->useBackendForStorage($storage)) {
67
						$foundBackend = $backend;
68
						$foundType = $type;
69
					}
70
				}
71
			}
72
		}
73
74
		if ($foundType === '' || $foundBackend === null) {
0 ignored issues
show
introduced by
The condition $foundType === '' is always true.
Loading history...
75
			throw new BackendNotFoundException("Version backend for $fullType not found");
76
		} else {
77
			return $foundBackend;
78
		}
79
	}
80
81
	public function getVersionsForFile(IUser $user, FileInfo $file): array {
82
		$backend = $this->getBackendForStorage($file->getStorage());
83
		return $backend->getVersionsForFile($user, $file);
84
	}
85
86
	public function createVersion(IUser $user, FileInfo $file) {
87
		$backend = $this->getBackendForStorage($file->getStorage());
88
		$backend->createVersion($user, $file);
89
	}
90
91
	public function rollback(IVersion $version) {
92
		$backend = $version->getBackend();
93
		return $backend->rollback($version);
94
	}
95
96
	public function read(IVersion $version) {
97
		$backend = $version->getBackend();
98
		return $backend->read($version);
99
	}
100
101
	public function getVersionFile(IUser $user, FileInfo $sourceFile, int $revision): File {
102
		$backend = $this->getBackendForStorage($sourceFile->getStorage());
103
		return $backend->getVersionFile($user, $sourceFile, $revision);
104
	}
105
106
	public function useBackendForStorage(IStorage $storage): bool {
107
		return false;
108
	}
109
}
110