Completed
Push — master ( ed3b9d...3835d9 )
by Roeland
20:08
created

Cache::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

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

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
77
		} else {
78
			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...
79
		}
80
	}
81
82
	protected function formatCacheEntry($entry) {
83
		$path = $entry['path'];
84
		$entry = parent::formatCacheEntry($entry);
85
		$sharePermissions = $this->storage->getPermissions($path);
86
		if (isset($entry['permissions'])) {
87
			$entry['permissions'] &= $sharePermissions;
88
		} else {
89
			$entry['permissions'] = $sharePermissions;
90
		}
91
		$entry['uid_owner'] = $this->storage->getOwner($path);
92
		$entry['displayname_owner'] = \OC_User::getDisplayName($entry['uid_owner']);
93
		if ($path === '') {
94
			$entry['is_share_mount_point'] = true;
95
		}
96
		return $entry;
97
	}
98
99
	/**
100
	 * remove all entries for files that are stored on the storage from the cache
101
	 */
102
	public function clear() {
103
		// Not a valid action for Shared Cache
104
	}
105
}
106