ViewInfoCache   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 118
ccs 44
cts 44
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInfoByPath() 0 7 2
A findInfoByPath() 0 14 2
A getInfoById() 0 11 3
A findInfoById() 0 43 4
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
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, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Activity;
23
24
use OC\Files\View;
25
use OCP\Files\NotFoundException;
26
27
class ViewInfoCache {
28
29
	/** @var array */
30
	protected $cachePath;
31
	/** @var array */
32
	protected $cacheId;
33
34
	/** @var \OC\Files\View */
35
	protected $view;
36
37
	/**
38
	 * @param View $view
39
	 */
40 28
	public function __construct(View $view) {
41 28
		$this->view = $view;
42 28
	}
43
44
	/**
45
	 * @param string $user
46
	 * @param string $path
47
	 * @return array
48
	 */
49 8
	public function getInfoByPath($user, $path) {
50 8
		if (isset($this->cachePath[$user][$path])) {
51 2
			return $this->cachePath[$user][$path];
52
		}
53
54 7
		return $this->findInfoByPath($user, $path);
55
	}
56
57
	/**
58
	 * @param string $user
59
	 * @param int $fileId
60
	 * @param string $path
61
	 * @return array
62
	 */
63 5
	public function getInfoById($user, $fileId, $path) {
64 5
		if (isset($this->cacheId[$user][$fileId])) {
65 2
			$cache = $this->cacheId[$user][$fileId];
66 2
			if ($cache['path'] === null) {
67 1
				$cache['path'] = $path;
68
			}
69 2
			return $cache;
70
		}
71
72 3
		return $this->findInfoById($user, $fileId, $path);
73
	}
74
75
	/**
76
	 * @param string $user
77
	 * @param string $path
78
	 * @return array
79
	 */
80 7
	protected function findInfoByPath($user, $path) {
81 7
		$this->view->chroot('/' . $user . '/files');
82
83 7
		$exists = $this->view->file_exists($path);
84
85 7
		$this->cachePath[$user][$path] = [
86 7
			'path'		=> $path,
87 7
			'exists'	=> $exists,
88 7
			'is_dir'	=> $exists ? $this->view->is_dir($path) : false,
89 7
			'view'		=> '',
90
		];
91
92 7
		return $this->cachePath[$user][$path];
93
	}
94
95
	/**
96
	 * @param string $user
97
	 * @param int $fileId
98
	 * @param string $filePath
99
	 * @return array
100
	 */
101 5
	protected function findInfoById($user, $fileId, $filePath) {
102 5
		$this->view->chroot('/' . $user . '/files');
103
104
		$cache = [
105 5
			'path'		=> $filePath,
106
			'exists'	=> false,
107
			'is_dir'	=> false,
108 5
			'view'		=> '',
109
		];
110
111 5
		$notFound = false;
112
		try {
113 5
			$path = $this->view->getPath($fileId);
114
115 2
			$cache['path'] = $path;
116 2
			$cache['is_dir'] = $this->view->is_dir($path);
117 2
			$cache['exists'] = true;
118 3
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
119
			// The file was not found in the normal view, maybe it is in
120
			// the trashbin?
121 3
			$this->view->chroot('/' . $user . '/files_trashbin');
122
123
			try {
124 3
				$path = $this->view->getPath($fileId);
125
126
				$cache = [
127 2
					'path'		=> \substr($path, \strlen('/files')),
128
					'exists'	=> true,
129 2
					'is_dir'	=> $this->view->is_dir($path),
130 2
					'view'		=> 'trashbin',
131
				];
132 1
			} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
133 1
				$notFound = true;
134
			}
135
		}
136
137 5
		$this->cacheId[$user][$fileId] = $cache;
138 5
		if ($notFound) {
139 1
			$this->cacheId[$user][$fileId]['path'] = null;
140
		}
141
142 5
		return $cache;
143
	}
144
}
145