Passed
Push — hotfix/fix-counts ( b4ff8e...673622 )
by Paul
04:39
created

ReviewsHtml::buildPagination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 9
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html;
4
5
use ArrayObject;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Modules\Html\Partial;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
use GeminiLabs\SiteReviews\Reviews;
10
11
class ReviewsHtml extends ArrayObject
12
{
13
	/**
14
	 * @var array
15
	 */
16
	public $args;
17
18
	/**
19
	 * @var string
20
	 */
21
	public $navigation;
22
23
	/**
24
	 * @var int
25
	 */
26
	public $pages;
27
28
	/**
29
	 * @var array
30
	 */
31
	public $reviews;
32
33
	public function __construct( array $reviews, $maxPageCount, array $args )
34
	{
35
		$this->args = $args;
36
		$this->pages = $maxPageCount;
37
		$this->reviews = $reviews;
38
		$this->navigation = $this->buildPagination();
39
		parent::__construct( $reviews, ArrayObject::STD_PROP_LIST|ArrayObject::ARRAY_AS_PROPS );
40
	}
41
42
	/**
43
	 * @return string
44
	 */
45
	public function __get( $key )
46
	{
47
		return array_key_exists( $key, $this->reviews )
48
			? $this->reviews[$key]
49
			: '';
50
	}
51
52
	/**
53
	 * @return string
54
	 */
55
	public function __toString()
56
	{
57
		return glsr( Template::class )->build( 'templates/reviews', [
58
			'args' => $this->args,
59
			'context' => [
60
				'assigned_to' => $this->args['assigned_to'],
61
				'category' => $this->args['category'],
62
				'class' => $this->getClass(),
63
				'id' => $this->args['id'],
64
				'navigation' => $this->getNavigation(),
65
				'reviews' => $this->getReviews(),
66
			],
67
		]);
68
	}
69
70
	/**
71
	 * @return string
72
	 */
73
	protected function buildPagination()
74
	{
75
		$pagination = glsr( Partial::class )->build( 'pagination', [
76
			'baseUrl' => glsr_get( $this->args, 'pagedUrl' ),
77
			'current' => glsr_get( $this->args, 'paged' ),
78
			'total' => $this->pages,
79
		]);
80
		$json = sprintf( '<glsr-pagination hidden data-atts=\'%s\'></glsr-pagination>', $this->args['json'] );
81
		return $pagination.$json;
82
	}
83
84
	/**
85
	 * @return string
86
	 */
87
	protected function getClass()
88
	{
89
		$defaults = [
90
			'glsr-reviews', 'glsr-default',
91
		];
92
		if( $this->args['pagination'] == 'ajax' ) {
93
			$defaults[] = 'glsr-ajax-pagination';
94
		}
95
		$classes = explode( ' ', $this->args['class'] );
96
		$classes = array_unique( array_merge( $defaults, $classes ));
97
		return implode( ' ', $classes );
98
	}
99
100
	/**
101
	 * @return string
102
	 */
103
	protected function getNavigation()
104
	{
105
		return wp_validate_boolean( $this->args['pagination'] )
106
			? $this->navigation
107
			: '';
108
	}
109
110
	/**
111
	 * @return string
112
	 */
113
	protected function getReviews()
114
	{
115
		return empty( $this->reviews )
116
			? $this->getReviewsFallback()
117
			: implode( PHP_EOL, $this->reviews );
118
	}
119
120
	/**
121
	 * @return string
122
	 */
123
	protected function getReviewsFallback()
124
	{
125
		if( empty( $this->args['fallback'] ) && glsr( OptionManager::class )->getBool( 'settings.reviews.fallback' )) {
126
			$this->args['fallback'] = __( 'There are no reviews yet. Be the first one to write one.', 'site-reviews' );
127
		}
128
		$fallback = '<p class="glsr-no-margins">'.$this->args['fallback'].'</p>';
129
		return apply_filters( 'site-reviews/reviews/fallback', $fallback, $this->args );
130
	}
131
}
132