Completed
Push — master ( c8f19a...ddbfd2 )
by Peter
72:09 queued 69:12
created

DataTrait::getData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 9
cp 0.8889
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 1
crap 4.0218
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Traits\DataProvider;
15
16
/**
17
 * DataTrait
18
 *
19
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
20
 */
21
trait DataTrait
22
{
23
24
	private $data = null;
25
26
	/**
27
	 * Returns the data items currently available, ensures that result is at leas empty array
28
	 * @param boolean $refresh whether the data should be re-fetched from persistent storage.
29
	 * @return array the list of data items currently available in this data provider.
30
	 */
31 4
	public function getData($refresh = false)
32
	{
33 4
		if ($this->data === null || $refresh)
34 4
		{
35 4
			$this->data = $this->fetchData();
36 4
		}
37 4
		if ($this->data === null)
38 4
		{
39
			return [];
40
		}
41 4
		return $this->data;
42
	}
43
44
	/**
45
	 * Manually set data. This is for special cases only,
46
	 * usually should not be used.
47
	 * 
48
	 * @param array $data
49
	 */
50
	public function setData($data)
51
	{
52
		$this->data = $data;
53
	}
54
55
	abstract protected function fetchData();
56
}
57