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

EntityDirLocator::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 9.6666
c 0
b 0
f 0
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $bucket_size of type null|integer is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
71 310
			$bucket_size = self::BUCKET_SIZE;
72
		}
73 310
		if ($guid < 1) {
74
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
75
		}
76 310
		return (int) max(floor($guid / $bucket_size) * $bucket_size, 1);
77
	}
78
}
79