Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/Elgg/EntityDirLocator.php (1 issue)

Checks if properties have been declared.

Best Practice Bug Major
1
<?php
2
namespace Elgg;
3
4
/**
5
 * Locate the relative path of an entity's data dir.
6
 *
7
 * This returns paths like: '1/27/'.
8
 *
9
 * @note This class does not require the Elgg engine to be loaded and is suitable for
10
 *       being used directly.
11
 *
12
 * @access private
13
 *
14
 * @package Elgg.Core
15
 */
16
class EntityDirLocator {
17
18
	/**
19
	 * Number of entries per matrix dir. DO NOT CHANGE!
20
	 */
21
	const BUCKET_SIZE = 5000;
22
23
	/**
24
	 * Find an entity's data dir.
25
	 *
26
	 * @param int $guid GUID of the entity.
27
	 *
28
	 * @throws \InvalidArgumentException
29
	 */
30 316
	public function __construct($guid) {
31 316
		$guid = (int) $guid;
32
33 316
		if (!$guid || $guid < 1) {
34
			// Don't throw a ClassException to keep this class completely atomic.
35 5
			throw new \InvalidArgumentException("GUIDs must be integers > 0.");
36
		}
37
38 311
		$this->guid = $guid;
0 ignored issues
show
Bug Best Practice introduced by
The property guid does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39 311
	}
40
	
41
	/**
42
	 * Construct a file path matrix for an entity.
43
	 * As of 1.9.0 matrixes are based on GUIDs and separated into dirs of 5000 entries
44
	 * with the dir name being the lower bound for the GUID.
45
	 *
46
	 * @return string The path with trailing '/' where the entity's data will be stored relative to the data dir.
47
	 */
48 310
	public function getPath() {
49 310
		$bound = $this->getLowerBucketBound($this->guid);
50 310
		return "$bound/$this->guid/";
51
	}
52
53
	/**
54
	 * String casting magic method.
55
	 *
56
	 * @return string
57
	 */
58 300
	public function __toString() {
59 300
		return $this->getPath();
60
	}
61
62
	/**
63
	 * Return the lower bound for a guid with a bucket size
64
	 *
65
	 * @param int $guid        The guid to get a bound for. Must be > 0.
66
	 * @param int $bucket_size The size of the bucket. (The number of entities per dir.)
67
	 * @return int
68
	 */
69 310
	private static function getLowerBucketBound($guid, $bucket_size = null) {
70 310
		if (!$bucket_size || $bucket_size < 1) {
71 310
			$bucket_size = self::BUCKET_SIZE;
72
		}
73 310
		if ($guid < 1) {
74
			return false;
75
		}
76 310
		return (int) max(floor($guid / $bucket_size) * $bucket_size, 1);
77
	}
78
}
79