Passed
Branch master (23c1c2)
by Wanderson
03:02
created

PaginationTrait::applyPagination()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Win\Repositories\Database\Orm;
4
5
use Win\Repositories\Pagination;
6
use Win\Repositories\Database\Sql\Query;
7
8
/**
9
 * Funcionalidade de paginação
10
 */
11
trait PaginationTrait
12
{
13
	/** @var Pagination */
14
	public $pagination;
15
16
	/** @var Query */
17
	protected $query;
18
19
	/** @return int */
20
	abstract public function count();
21
22
	/**
23
	 * @param int $pageSize
24
	 * @param int $pageNumber
25
	 */
26
	public function paginate($pageSize, $pageNumber = 1)
27
	{
28
		$this->pagination->setPage($pageSize, $pageNumber);
29
30
		return $this;
31
	}
32
33
	/**
34
	 * Define a paginação se necessário
35
	 */
36
	private function applyPagination()
37
	{
38
		$count = $this->count();
39
		$pagination = $this->pagination;
40
41
		if ($pagination->pageSize() && $count) {
42
			$pagination->setCount($count);
43
			$this->query->limit->set($pagination->offset(), $pagination->pageSize());
44
		}
45
	}
46
}
47