Completed
Push — master ( d2cc48...0dcb6b )
by Morris
37:29 queued 19:35
created

AppData   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 13.86 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 14
loc 101
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
C getAppDataFolder() 0 34 7
A getFolder() 0 6 1
A newFolder() 0 5 1
A getDirectoryListing() 14 14 2
A getId() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright 2016 Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OC\Files\AppData;
26
27
use OC\Files\SimpleFS\SimpleFolder;
28
use OCP\Files\IAppData;
29
use OCP\Files\IRootFolder;
30
use OCP\Files\Folder;
31
use OC\SystemConfig;
32
use OCP\Files\Node;
33
use OCP\Files\NotFoundException;
34
use OCP\Files\NotPermittedException;
35
use OCP\Files\SimpleFS\ISimpleFolder;
36
37
class AppData implements IAppData {
38
39
	/** @var IRootFolder */
40
	private $rootFolder;
41
42
	/** @var SystemConfig */
43
	private $config;
44
45
	/** @var string */
46
	private $appId;
47
48
	/** @var Folder */
49
	private $folder;
50
51
	/**
52
	 * AppData constructor.
53
	 *
54
	 * @param IRootFolder $rootFolder
55
	 * @param SystemConfig $systemConfig
56
	 * @param string $appId
57
	 */
58
	public function __construct(IRootFolder $rootFolder,
59
								SystemConfig $systemConfig,
60
								string $appId) {
61
62
		$this->rootFolder = $rootFolder;
63
		$this->config = $systemConfig;
64
		$this->appId = $appId;
65
	}
66
67
	/**
68
	 * @return Folder
69
	 * @throws \RuntimeException
70
	 */
71
	private function getAppDataFolder(): Folder {
72
		if ($this->folder === null) {
73
			$instanceId = $this->config->getValue('instanceid', null);
74
			if ($instanceId === null) {
75
				throw new \RuntimeException('no instance id!');
76
			}
77
78
			$name = 'appdata_' . $instanceId;
79
80
			try {
81
				$appDataFolder = $this->rootFolder->get($name);
82
			} catch (NotFoundException $e) {
83
				try {
84
					$appDataFolder = $this->rootFolder->newFolder($name);
85
				} catch (NotPermittedException $e) {
86
					throw new \RuntimeException('Could not get appdata folder');
87
				}
88
			}
89
90
			try {
91
				$appDataFolder = $appDataFolder->get($this->appId);
92
			} catch (NotFoundException $e) {
93
				try {
94
					$appDataFolder = $appDataFolder->newFolder($this->appId);
95
				} catch (NotPermittedException $e) {
96
					throw new \RuntimeException('Could not get appdata folder for ' . $this->appId);
97
				}
98
			}
99
100
			$this->folder = $appDataFolder;
101
		}
102
103
		return $this->folder;
104
	}
105
106
	public function getFolder(string $name): ISimpleFolder {
107
		$node = $this->getAppDataFolder()->get($name);
108
109
		/** @var Folder $node */
110
		return new SimpleFolder($node);
111
	}
112
113
	public function newFolder(string $name): ISimpleFolder {
114
		$folder = $this->getAppDataFolder()->newFolder($name);
115
116
		return new SimpleFolder($folder);
117
	}
118
119 View Code Duplication
	public function getDirectoryListing(): array {
120
		$listing = $this->getAppDataFolder()->getDirectoryListing();
121
122
		$fileListing = array_map(function(Node $folder) {
123
			if ($folder instanceof Folder) {
124
				return new SimpleFolder($folder);
125
			}
126
			return null;
127
		}, $listing);
128
129
		$fileListing = array_filter($fileListing);
130
131
		return array_values($fileListing);
132
	}
133
134
	public function getId(): int {
135
		return $this->getAppDataFolder()->getId();
136
	}
137
}
138