Completed
Push — master ( ff3e8c...ea9b1c )
by Lukas
13:22 queued 02:44
created

AppData   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 14.43 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

5 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

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
		$listing = $this->getAppDataFolder()->getDirectoryListing();
119
120
		$fileListing = array_map(function(Node $folder) {
121
			if ($folder instanceof Folder) {
122
				return new SimpleFolder($folder);
123
			}
124
			return null;
125
		}, $listing);
126
127
		$fileListing = array_filter($fileListing);
128
129
		return array_values($fileListing);
130
	}
131
}
132