ViewInfoCache::findInfoByPath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Activity;
24
25
26
use OC\Files\View;
27
use OCP\Files\NotFoundException;
28
29
class ViewInfoCache {
30
31
	/** @var array */
32
	protected $cachePath;
33
	/** @var array */
34
	protected $cacheId;
35
36
	/** @var \OC\Files\View */
37
	protected $view;
38
39
	/**
40
	 * @param View $view
41
	 */
42 38
	public function __construct(View $view) {
43 38
		$this->view = $view;
44 38
	}
45
46
	/**
47
	 * @param string $user
48
	 * @param string $path
49
	 * @return array
50
	 */
51 8
	public function getInfoByPath($user, $path) {
52 8
		if (isset($this->cachePath[$user][$path])) {
53 4
			return $this->cachePath[$user][$path];
54
		}
55
56 4
		return $this->findInfoByPath($user, $path);
57
	}
58
59
	/**
60
	 * @param string $user
61
	 * @param int $fileId
62
	 * @param string $path
63
	 * @return array
64
	 */
65 5
	public function getInfoById($user, $fileId, $path) {
66 5
		if (isset($this->cacheId[$user][$fileId])) {
67 2
			$cache = $this->cacheId[$user][$fileId];
68 2
			if ($cache['path'] === null) {
69 1
				$cache['path'] = $path;
70
			}
71 2
			return $cache;
72
		}
73
74 3
		return $this->findInfoById($user, $fileId, $path);
75
	}
76
77
	/**
78
	 * @param string $user
79
	 * @param string $path
80
	 * @return array
81
	 */
82 4
	protected function findInfoByPath($user, $path) {
83 4
		$this->view->chroot('/' . $user . '/files');
84
85 4
		$exists = $this->view->file_exists($path);
86
87 4
		$this->cachePath[$user][$path] = [
88 4
			'path'		=> $path,
89 4
			'exists'	=> $exists,
90 4
			'is_dir'	=> $exists ? $this->view->is_dir($path) : false,
91 4
			'view'		=> '',
92
		];
93
94 4
		return $this->cachePath[$user][$path];
95
	}
96
97
	/**
98
	 * @param string $user
99
	 * @param int $fileId
100
	 * @param string $filePath
101
	 * @return array
102
	 */
103 5
	protected function findInfoById($user, $fileId, $filePath) {
104 5
		$this->view->chroot('/' . $user . '/files');
105
106
		$cache = [
107 5
			'path'		=> $filePath,
108
			'exists'	=> false,
109
			'is_dir'	=> false,
110 5
			'view'		=> '',
111
		];
112
113 5
		$notFound = false;
114
		try {
115 5
			$path = $this->view->getPath($fileId);
116
117 2
			$cache['path'] = $path;
118 2
			$cache['is_dir'] = $this->view->is_dir($path);
119 2
			$cache['exists'] = true;
120 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...
121
			// The file was not found in the normal view, maybe it is in
122
			// the trashbin?
123 3
			$this->view->chroot('/' . $user . '/files_trashbin');
124
125
			try {
126 3
				$path = $this->view->getPath($fileId);
127
128
				$cache = [
129 2
					'path'		=> substr($path, strlen('/files')),
130
					'exists'	=> true,
131 2
					'is_dir'	=> $this->view->is_dir($path),
132 2
					'view'		=> 'trashbin',
133
				];
134 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...
135 1
				$notFound = true;
136
			}
137
		}
138
139 5
		$this->cacheId[$user][$fileId] = $cache;
140 5
		if ($notFound) {
141 1
			$this->cacheId[$user][$fileId]['path'] = null;
142
		}
143
144 5
		return $cache;
145
	}
146
}
147