Passed
Push — master ( 576dc5...fc7b10 )
by Paul
04:33
created

SiteReviewsSummary::buildSummaryText()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 17
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 0
crap 12
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html\Partials;
4
5
use GeminiLabs\SiteReviews\Database;
6
use GeminiLabs\SiteReviews\Modules\Html;
7
use GeminiLabs\SiteReviews\Modules\Html\Builder;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
use GeminiLabs\SiteReviews\Modules\Html\Partial;
10
use GeminiLabs\SiteReviews\Modules\Rating;
11
use GeminiLabs\SiteReviews\Modules\Schema;
12
13
class SiteReviewsSummary
14
{
15
	/**
16
	 * @var array
17
	 */
18
	protected $args;
19
20
	/**
21
	 * @var float
22
	 */
23
	protected $rating;
24
25
	/**
26
	 * @var object
27
	 */
28
	protected $reviews;
29
30
	/**
31
	 * @return void|string
32
	 */
33
	public function build( array $args = [] )
34
	{
35
		$this->args = $args;
36
		$this->reviews = glsr( Database::class )->getReviews( $args )->results;
37
		if( empty( $this->reviews ) && $this->isHidden( 'if_empty' ))return;
38
		$this->rating = glsr( Rating::class )->getAverage( $this->reviews );
39
		$this->generateSchema();
40
		glsr_debug( $args );
41
		return glsr( Template::class )->build( 'templates/reviews-summary', [
42
			'context' => [
43
				'class' => $this->getClass(),
44
				'id' => $this->args['id'],
45
				'percentage' => $this->buildPercentage(),
46
				'rating' => $this->buildRating(),
47
				'stars' => $this->buildStars(),
48
				'text' => $this->buildText(),
49
			],
50
		]);
51
	}
52
53
	/**
54
	 * @return void|string
55
	 */
56
	protected function buildPercentage()
57
	{
58
		if( $this->isHidden( 'bars' ))return;
59
		$range = range( Rating::MAX_RATING, 1 );
60
		$percentages = preg_filter( '/$/', '%', glsr( Rating::class )->getPercentages( $this->reviews ));
61
		$bars = array_reduce( $range, function( $carry, $level ) use( $percentages ) {
62
			$label = $this->buildPercentageLabel( $level );
63
			$background = $this->buildPercentageBackground( $percentages[$level] );
64
			$percent = $this->buildPercentagePercent( $percentages[$level] );
65
			return $carry.glsr( Builder::class )->div( $label.$background.$percent, [
66
				'class' => 'glsr-bar',
67
			]);
68
		});
69
		return $this->wrap( 'percentage', $bars );
70
	}
71
72
	/**
73
	 * @param string $percent
74
	 * @return string
75
	 */
76
	protected function buildPercentageBackground( $percent )
77
	{
78
		$backgroundPercent = glsr( Builder::class )->span([
79
			'class' => 'glsr-bar-background-percent',
80
			'style' => 'width:'.$percent,
81
		]);
82
		return glsr( Builder::class )->span( $backgroundPercent, [
83
			'class' => 'glsr-bar-background',
84
		]);
85
	}
86
87
	/**
88
	 * @param int $level
89
	 * @return string
90
	 */
91
	protected function buildPercentageLabel( $level )
92
	{
93
		return glsr( Builder::class )->span( $this->args['labels'][$level], [
94
			'class' => 'glsr-bar-label',
95
		]);
96
	}
97
98
	/**
99
	 * @param string $percent
100
	 * @return string
101
	 */
102
	protected function buildPercentagePercent( $percent )
103
	{
104
		return glsr( Builder::class )->span( $percent, [
105
			'class' => 'glsr-bar-percent',
106
		]);
107
	}
108
109
	/**
110
	 * @return void|string
111
	 */
112
	protected function buildRating()
113
	{
114
		if( $this->isHidden( 'rating' ))return;
115
		return $this->wrap( 'rating', '<span>'.$this->rating.'</span>' );
116
	}
117
118
	/**
119
	 * @return void|string
120
	 */
121
	protected function buildStars()
122
	{
123
		if( $this->isHidden( 'stars' ))return;
124
		$stars = glsr( Partial::class )->build( 'star-rating', [
125
			'number' => count( $this->reviews ),
126
			'rating' => $this->rating,
127
		]);
128
		return $this->wrap( 'stars', $stars );
129
	}
130
131
	/**
132
	 * @return void|string
133
	 */
134
	protected function buildText()
135
	{
136
		if( $this->isHidden( 'summary' ))return;
137
		$count = count( $this->reviews );
138
		if( empty( $this->args['text'] )) {
139
			 $this->args['text'] = _nx(
140
				'{rating} out of {max} stars (based on %d review)',
141
				'{rating} out of {max} stars (based on %d reviews)',
142
				$count,
143
				'Do not translate {rating} and {max}, they are template tags.',
144
				'site-reviews'
145
			);
146
		}
147
		$summary = str_replace( ['{rating}','{max}'], [$this->rating, Rating::MAX_RATING], $this->args['text'] );
148
		$summary = str_replace( ['%s','%d'], $count, $summary );
149
		return $this->wrap( 'text', '<span>'.$summary.'</span>' );
150
	}
151
152
	/**
153
	 * @return void
154
	 */
155
	protected function generateSchema()
156
	{
157
		if( !wp_validate_boolean( $this->args['schema'] ))return;
158
		glsr( Schema::class )->store( glsr( Schema::class )->buildSummary( $this->args ));
159
	}
160
161
	/**
162
	 * @return string
163
	 */
164
	protected function getClass()
165
	{
166
		$style = apply_filters( 'site-reviews/style', 'glsr-style' );
167
		return trim( 'glsr-summary '.$style.' '.$this->args['class'] );
168
	}
169
170
	/**
171
	 * @param string $key
172
	 * @return bool
173
	 */
174
	protected function isHidden( $key )
175
	{
176
		return in_array( $key, $this->args['hide'] );
177
	}
178
179
	/**
180
	 * @param string $key
181
	 * @param string $value
182
	 * @return string
183
	 */
184
	protected function wrap( $key, $value )
185
	{
186
		return glsr( Builder::class )->div( $value, [
187
			'class' => 'glsr-summary-'.$key,
188
		]);
189
	}
190
}
191