Completed
Push — master ( 5bc8c9...07e638 )
by Morris
52:34 queued 36:57
created

Cache::getOwnerDisplayName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 9.4285
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
36
/**
37
 * Metadata cache for shared files
38
 *
39
 * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
40
 */
41
class Cache extends CacheJail {
42
	/**
43
	 * @var \OCA\Files_Sharing\SharedStorage
44
	 */
45
	private $storage;
46
47
	/**
48
	 * @var ICacheEntry
49
	 */
50
	private $sourceRootInfo;
51
52
	private $rootUnchanged = true;
53
54
	private $ownerDisplayName;
55
56
	private $numericId;
57
58
	/**
59
	 * @param \OCA\Files_Sharing\SharedStorage $storage
60
	 * @param ICacheEntry $sourceRootInfo
61
	 */
62
	public function __construct($storage, ICacheEntry $sourceRootInfo) {
63
		$this->storage = $storage;
64
		$this->sourceRootInfo = $sourceRootInfo;
65
		$this->numericId = $sourceRootInfo->getStorageId();
66
67
		parent::__construct(
68
			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...
69
			null
70
		);
71
	}
72
73
	protected function getRoot() {
74
		if (is_null($this->root)) {
75
			$absoluteRoot = $this->sourceRootInfo->getPath();
76
77
			// the sourceRootInfo path is the absolute path of the folder in the "real" storage
78
			// in the case where a folder is shared from a Jail we need to ensure that the share Jail
79
			// has it's root set relative to the source Jail
80
			$currentStorage = $this->storage->getSourceStorage();
81
			if ($currentStorage->instanceOfStorage(Jail::class)) {
82
				/** @var Jail $currentStorage */
83
				$absoluteRoot = $currentStorage->getJailedPath($absoluteRoot);
84
			}
85
			$this->root = $absoluteRoot;
86
		}
87
		return $this->root;
88
	}
89
90
	public function getCache() {
91
		if (is_null($this->cache)) {
92
			$sourceStorage = $this->storage->getSourceStorage();
93
			if ($sourceStorage) {
94
				$this->cache = $sourceStorage->getCache();
95
			} else {
96
				// don't set $this->cache here since sourceStorage will be set later
97
				return new FailedCache();
98
			}
99
		}
100
		return $this->cache;
101
	}
102
103
	public function getNumericStorageId() {
104
		if (isset($this->numericId)) {
105
			return $this->numericId;
106
		} else {
107
			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...
108
		}
109
	}
110
111
	public function get($file) {
112
		if ($this->rootUnchanged && ($file === '' || $file === $this->sourceRootInfo->getId())) {
113
			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...
114
		}
115
		return parent::get($file);
116
	}
117
118
	public function update($id, array $data) {
119
		$this->rootUnchanged = false;
120
		parent::update($id, $data);
121
	}
122
123
	public function insert($file, array $data) {
124
		$this->rootUnchanged = false;
125
		return parent::insert($file, $data);
126
	}
127
128
	public function remove($file) {
129
		$this->rootUnchanged = false;
130
		parent::remove($file);
131
	}
132
133
	public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
134
		$this->rootUnchanged = false;
135
		return parent::moveFromCache($sourceCache, $sourcePath, $targetPath);
136
	}
137
138
	protected function formatCacheEntry($entry, $path = null) {
139
		if (is_null($path)) {
140
			$path = isset($entry['path']) ? $entry['path'] : '';
141
			$entry['path'] = $this->getJailedPath($path);
142
		} else {
143
			$entry['path'] = $path;
144
		}
145
		$sharePermissions = $this->storage->getPermissions($path);
146
		if (isset($entry['permissions'])) {
147
			$entry['permissions'] &= $sharePermissions;
148
		} else {
149
			$entry['permissions'] = $sharePermissions;
150
		}
151
		$entry['uid_owner'] = $this->storage->getOwner('');
152
		$entry['displayname_owner'] = $this->getOwnerDisplayName();
153
		if ($path === '') {
154
			$entry['is_share_mount_point'] = true;
155
		}
156
		return $entry;
157
	}
158
159
	private function getOwnerDisplayName() {
160
		if (!$this->ownerDisplayName) {
161
			$this->ownerDisplayName = \OC_User::getDisplayName($this->storage->getOwner(''));
162
		}
163
		return $this->ownerDisplayName;
164
	}
165
166
	/**
167
	 * remove all entries for files that are stored on the storage from the cache
168
	 */
169
	public function clear() {
170
		// Not a valid action for Shared Cache
171
	}
172
}
173