DataProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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;
15
16
use Maslosoft\Mangan\Interfaces\DataProviderInterface;
17
use Maslosoft\Mangan\Interfaces\FinderInterface;
18
use Maslosoft\Mangan\Traits\DataProvider\ConfigureTrait;
19
use Maslosoft\Mangan\Traits\DataProvider\CriteriaTrait;
20
use Maslosoft\Mangan\Traits\DataProvider\DataTrait;
21
use Maslosoft\Mangan\Traits\DataProvider\PaginationTrait;
22
use Maslosoft\Mangan\Traits\ModelAwareTrait;
23
use Maslosoft\Mangan\Traits\SortAwareTrait;
24
25
/**
26
 * Mongo document data provider
27
 *
28
 * Implements a data provider based on Document.
29
 *
30
 * DataProvider provides data in terms of Document objects which are
31
 * of class {@link modelClass}. It uses the AR {@link EntityManager::findAll} method
32
 * to retrieve the data from database. The {@link query} property can be used to
33
 * specify various query options, such as conditions, sorting, pagination, etc.
34
 *
35
 * @author Ianaré Sévi
36
 * @author Dariusz Górecki <[email protected]>
37
 * @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
38
 * @copyright 2011 CleverIT http://www.cleverit.com.pl
39
 * @since v1.0
40
 */
41
class DataProvider implements DataProviderInterface
42
{
43
44
	use ConfigureTrait,
45
	  CriteriaTrait,
46
	  DataTrait,
47
	  ModelAwareTrait,
48
	  PaginationTrait,
49
	  SortAwareTrait;
50
51
	const CriteriaClass = Criteria::class;
52
53
	/**
54
	 * Finder instance
55
	 * @var FinderInterface|Finder
56
	 */
57
	private $finder = null;
58
	private $totalItemCount = null;
59
60
	/**
61
	 * Constructor.
62
	 * @param mixed $modelClass the model class (e.g. 'Post') or the model finder instance
63
	 * (e.g. <code>Post::model()</code>, <code>Post::model()->published()</code>).
64
	 * @param array $config configuration (name=>value) to be applied as the initial property values of this class.
65
	 * @since v1.0
66
	 */
67 6
	public function __construct($modelClass, $config = [])
68
	{
69 6
		$this->configure($modelClass, $config);
70 6
		$this->finder = Finder::create($this->getModel());
71 6
	}
72
73
	/**
74
	 * Returns the number of data items in the current page.
75
	 * This is equivalent to <code>count($provider->getData())</code>.
76
	 * When {@link pagination} is set false, this returns the same value as {@link totalItemCount}.
77
	 * @param boolean $refresh whether the number of data items should be re-calculated.
78
	 * @return integer the number of data items in the current page.
79
	 */
80
	public function getItemCount($refresh = false)
81
	{
82
		return count($this->getData($refresh));
83
	}
84
85
	/**
86
	 * Returns the total number of data items.
87
	 * When {@link pagination} is set false, this returns the same value as {@link itemCount}.
88
	 * @return integer total number of possible data items.
89
	 */
90 6
	public function getTotalItemCount()
91
	{
92 6
		if ($this->totalItemCount === null)
93
		{
94 6
			$this->totalItemCount = $this->finder->count($this->getCriteria());
95
		}
96 6
		return $this->totalItemCount;
97
	}
98
99
	/**
100
	 * Fetches the data from the persistent data storage.
101
	 * @return Document[]|Cursor list of data items
102
	 * @since v1.0
103
	 */
104 6
	protected function fetchData()
105
	{
106 6
		$criteria = $this->configureFetch();
107
108
		// Finally apply all to finder
109 6
		return $this->finder->findAll($criteria);
110
	}
111
112
}
113