Passed
Push — master ( 6d3e46...96e47d )
by Pauli
02:27
created

Random   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 72
ccs 0
cts 26
cp 0
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndices() 0 17 4
A decodeIndices() 0 5 2
A encodeIndices() 0 2 1
A __construct() 0 3 1
A pickItems() 0 8 2
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2020
11
 */
12
13
namespace OCA\Music\Utility;
14
15
use \OCA\Music\AppFramework\Core\Logger;
16
use \OCA\Music\Db\Cache;
17
18
19
class Random {
20
	private $cache;
21
	private $logger;
22
23
	public function __construct(Cache $cache, Logger $logger) {
24
		$this->cache = $cache;
25
		$this->logger = $logger;
26
	}
27
28
	/**
29
	 * Get desired number of random items from the given array
30
	 *
31
	 * @param array $itemArray
32
	 * @param int $count
33
	 * @return array
34
	 */
35
	public static function pickItems($itemArray, $count) {
36
		$count = \min($count, \count($itemArray)); // can't return more than all items
37
		$indices = \array_rand($itemArray, $count);
38
		if ($count == 1) { // return type is not array when randomizing a single index
39
			$indices = [$indices];
40
		}
41
42
		return Util::arrayMultiGet($itemArray, $indices);
43
	}
44
45
	/**
46
	 * Get desired number of random array indices. This function supports paging
47
	 * so that all the indices can be browsed through page-by-page, without getting
48
	 * the same index more than once. This requires persistence and identifying the
49
	 * logical array in question. The array is identified by the user ID and a free
50
	 * text identifier supplied by the caller.
51
	 * 
52
	 * For a single logical array, the indices are shuffled every time when the
53
	 * page 0 is requested. Also, if the size of the array in question has changed
54
	 * since the previous call, then the indices are reshuffled.
55
	 *
56
	 * @param int $arrSize
57
	 * @param int $offset
58
	 * @param int $count
59
	 * @param string $userId
60
	 * @param string $arrId
61
	 * @return int[]
62
	 */
63
	public function getIndices($arrSize, $offset, $count, $userId, $arrId) {
64
		$cacheKey = 'random_indices_' . $arrId;
65
66
		$indices = self::decodeIndices($this->cache->get($userId, $cacheKey));
67
68
		// reshuffle if necessary
69
		if ($offset == 0 || \count($indices) != $arrSize) {
70
			if ($arrSize > 0) {
71
				$indices = \range(0, $arrSize - 1);
72
			} else {
73
				$indices = [];
74
			}
75
			\shuffle($indices);
76
			$this->cache->set($userId, $cacheKey, self::encodeIndices($indices));
77
		}
78
79
		return \array_slice($indices, $offset, $count);
80
	}
81
82
	private static function encodeIndices($indices) {
83
		return \implode(',', $indices);
84
	}
85
86
	private static function decodeIndices($buffer) {
87
		if (empty($buffer)) {
88
			return [];
89
		} else {
90
			return \explode(',', $buffer);
91
		}
92
	}
93
}
94