Completed
Push — master ( 2f4504...89fdd0 )
by Morris
18:06
created

Cache::formatCacheEntry()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 24
nop 2
dl 0
loc 27
rs 8.8657
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Christopher Schäpers <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Jörn Friedrich Dreyer <[email protected]>
8
 * @author Michael Gapczynski <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 *
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
namespace OCA\Files_Sharing;
30
31
use OC\Files\Cache\FailedCache;
32
use OC\Files\Cache\Wrapper\CacheJail;
33
use OC\Files\Storage\Wrapper\Jail;
34
use OCP\Files\Cache\ICacheEntry;
35
use OCP\Files\StorageNotAvailableException;
36
37
/**
38
 * Metadata cache for shared files
39
 *
40
 * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
41
 */
42
class Cache extends CacheJail {
43
	/**
44
	 * @var \OCA\Files_Sharing\SharedStorage
45
	 */
46
	private $storage;
47
48
	/**
49
	 * @var ICacheEntry
50
	 */
51
	private $sourceRootInfo;
52
53
	private $rootUnchanged = true;
54
55
	private $ownerDisplayName;
56
57
	private $numericId;
58
59
	/**
60
	 * @param \OCA\Files_Sharing\SharedStorage $storage
61
	 * @param ICacheEntry $sourceRootInfo
62
	 */
63
	public function __construct($storage, ICacheEntry $sourceRootInfo) {
64
		$this->storage = $storage;
65
		$this->sourceRootInfo = $sourceRootInfo;
66
		$this->numericId = $sourceRootInfo->getStorageId();
67
68
		parent::__construct(
69
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<OCP\Files\Cache\ICache>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
			null
71
		);
72
	}
73
74
	protected function getRoot() {
75
		if (is_null($this->root)) {
76
			$absoluteRoot = $this->sourceRootInfo->getPath();
77
78
			// the sourceRootInfo path is the absolute path of the folder in the "real" storage
79
			// in the case where a folder is shared from a Jail we need to ensure that the share Jail
80
			// has it's root set relative to the source Jail
81
			$currentStorage = $this->storage->getSourceStorage();
82
			if ($currentStorage->instanceOfStorage(Jail::class)) {
83
				/** @var Jail $currentStorage */
84
				$absoluteRoot = $currentStorage->getJailedPath($absoluteRoot);
85
			}
86
			$this->root = $absoluteRoot;
87
		}
88
		return $this->root;
89
	}
90
91
	public function getCache() {
92
		if (is_null($this->cache)) {
93
			$sourceStorage = $this->storage->getSourceStorage();
94
			if ($sourceStorage) {
95
				$this->cache = $sourceStorage->getCache();
96
			} else {
97
				// don't set $this->cache here since sourceStorage will be set later
98
				return new FailedCache();
99
			}
100
		}
101
		return $this->cache;
102
	}
103
104
	public function getNumericStorageId() {
105
		if (isset($this->numericId)) {
106
			return $this->numericId;
107
		} else {
108
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface OCP\Files\Cache\ICache::getNumericStorageId of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
109
		}
110
	}
111
112
	public function get($file) {
113
		if ($this->rootUnchanged && ($file === '' || $file === $this->sourceRootInfo->getId())) {
114
			return $this->formatCacheEntry(clone $this->sourceRootInfo, '');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->formatCach...s->sourceRootInfo, ''); (array) is incompatible with the return type declared by the interface OCP\Files\Cache\ICache::get of type OCP\Files\Cache\ICacheEntry|false.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
115
		}
116
		return parent::get($file);
117
	}
118
119
	public function update($id, array $data) {
120
		$this->rootUnchanged = false;
121
		parent::update($id, $data);
122
	}
123
124
	public function insert($file, array $data) {
125
		$this->rootUnchanged = false;
126
		return parent::insert($file, $data);
127
	}
128
129
	public function remove($file) {
130
		$this->rootUnchanged = false;
131
		parent::remove($file);
132
	}
133
134
	public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
135
		$this->rootUnchanged = false;
136
		return parent::moveFromCache($sourceCache, $sourcePath, $targetPath);
137
	}
138
139
	protected function formatCacheEntry($entry, $path = null) {
140
		if (is_null($path)) {
141
			$path = isset($entry['path']) ? $entry['path'] : '';
142
			$entry['path'] = $this->getJailedPath($path);
143
		} else {
144
			$entry['path'] = $path;
145
		}
146
147
		try {
148
			$sharePermissions = $this->storage->getPermissions($entry['path']);
149
		} catch (StorageNotAvailableException $e) {
150
			// thrown by FailedStorage e.g. when the sharer does not exist anymore
151
			// (IDE may say the exception is never thrown – false negative)
152
			$sharePermissions = 0;
153
		}
154
		if (isset($entry['permissions'])) {
155
			$entry['permissions'] &= $sharePermissions;
156
		} else {
157
			$entry['permissions'] = $sharePermissions;
158
		}
159
		$entry['uid_owner'] = $this->storage->getOwner('');
160
		$entry['displayname_owner'] = $this->getOwnerDisplayName();
161
		if ($path === '') {
162
			$entry['is_share_mount_point'] = true;
163
		}
164
		return $entry;
165
	}
166
167
	private function getOwnerDisplayName() {
168
		if (!$this->ownerDisplayName) {
169
			$this->ownerDisplayName = \OC_User::getDisplayName($this->storage->getOwner(''));
170
		}
171
		return $this->ownerDisplayName;
172
	}
173
174
	/**
175
	 * remove all entries for files that are stored on the storage from the cache
176
	 */
177
	public function clear() {
178
		// Not a valid action for Shared Cache
179
	}
180
}
181