Completed
Push — master ( 801ea1...6fb532 )
by Peter
06:45
created

DataTrait::getData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 1
crap 4.074
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Mangan\Traits\DataProvider;
10
11
/**
12
 * DataTrait
13
 *
14
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
15
 */
16
trait DataTrait
17
{
18
19
	private $data = null;
20
21
	/**
22
	 * Returns the data items currently available, ensures that result is at leas empty array
23
	 * @param boolean $refresh whether the data should be re-fetched from persistent storage.
24
	 * @return array the list of data items currently available in this data provider.
25
	 */
26 1
	public function getData($refresh = false)
27
	{
28 1
		if ($this->data === null || $refresh)
29
		{
30 1
			$this->data = $this->fetchData();
31
		}
32 1
		if ($this->data === null)
33
		{
34
			return [];
35
		}
36 1
		return $this->data;
37
	}
38
39
	/**
40
	 * Manually set data. This is for special cases only,
41
	 * usually should not be used.
42
	 * 
43
	 * @param array $data
44
	 */
45
	public function setData($data)
46
	{
47
		$this->data = $data;
48
	}
49
50
	abstract protected function fetchData();
51
}
52