Test Failed
Push — master ( 21e3e7...365a0a )
by Paul
03:49
created

CountsManager::setTermCounts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
use GeminiLabs\SiteReviews\Database\SqlQueries;
7
use GeminiLabs\SiteReviews\Modules\Rating;
8
9
class CountsManager
10
{
11
	const META_COUNT = '_glsr_count';
12
13
	/**
14
	 * @param int $limit
15
	 * @return array
16
	 */
17
	public function buildCounts( $limit = 500 )
18
	{
19
		return $this->build( $limit );
20
	}
21
22
	/**
23
	 * @param int $postId
24
	 * @param int $limit
25
	 * @return array
26
	 */
27
	public function buildPostCounts( $postId, $limit = 500 )
28
	{
29
		return $this->build( $limit, ['post_id' => $postId] );
30
	}
31
32
	/**
33
	 * @param int $termId
34
	 * @param int $limit
35
	 * @return array
36
	 */
37
	public function buildTermCounts( $termId, $limit = 500 )
38
	{
39
		return $this->build( $limit, ['term_id' => $termId] );
40
	}
41
42
	/**
43
	 * @param string $type
44
	 * @param int $rating
45
	 * @return array
46
	 */
47
	public function decrease( array $reviewCounts, $type, $rating )
48
	{
49
		if( isset( $reviewCounts[$type][$rating] )) {
50
			$reviewCounts[$type][$rating] = max( 0, $reviewCounts[$type][$rating] - 1 );
51
		}
52
		return $reviewCounts;
53
	}
54
55
	/**
56
	 * @return array
57
	 */
58
	public function flatten( array $reviewCounts )
59
	{
60
		$counts = [];
61
		array_walk_recursive( $reviewCounts, function( $num, $index ) use( &$counts ) {
62
			$counts[$index] = isset( $counts[$index] )
63
				? $num + $counts[$index]
64
				: $num;
65
		});
66
		return $counts;
67
	}
68
69
	/**
70
	 * @return array
71
	 */
72
	public function getCounts()
73
	{
74
		return glsr( OptionManager::class )->get( 'count', [] );
75
	}
76
77
	/**
78
	 * @param int $postId
79
	 * @return array
80
	 */
81
	public function getPostCounts( $postId )
82
	{
83
		return (array)get_post_meta( $postId, static::META_COUNT, true );
84
	}
85
86
	/**
87
	 * @param int $termId
88
	 * @return array
89
	 */
90
	public function getTermCounts( $termId )
91
	{
92
		return (array)get_term_meta( $termId, static::META_COUNT, true );
93
	}
94
95
	/**
96
	 * @param string $type
97
	 * @param int $rating
98
	 * @return array
99
	 */
100
	public function increase( array $reviewCounts, $type, $rating )
101
	{
102
		if( !array_key_exists( $type, glsr()->reviewTypes )) {
103
			return $reviewCounts;
104
		}
105
		if( !array_key_exists( $type, $reviewCounts )) {
106
			$reviewCounts[$type] = [];
107
		}
108
		$reviewCounts = $this->normalize( $reviewCounts );
109
		$reviewCounts[$type][$rating] = intval( $reviewCounts[$type][$rating] ) + 1;
110
		return $reviewCounts;
111
	}
112
113
	/**
114
	 * @return void
115
	 */
116
	public function setCounts( array $reviewCounts )
117
	{
118
		glsr( OptionManager::class )->set( 'count', $reviewCounts );
119
	}
120
121
	/**
122
	 * @param int $postId
123
	 * @return void
124
	 */
125
	public function setPostCounts( $postId, array $reviewCounts )
126
	{
127
		update_post_meta( $postId, static::META_COUNT, $reviewCounts );
128
	}
129
130
	/**
131
	 * @param int $termId
132
	 * @return void
133
	 */
134
	public function setTermCounts( $termId, array $reviewCounts )
135
	{
136
		update_term_meta( $termId, static::META_COUNT, $reviewCounts );
137
	}
138
139
	/**
140
	 * @param int $limit
141
	 * @return array
142
	 */
143
	protected function build( $limit, array $args = [] )
144
	{
145
		$counts = [];
146
		$lastPostId = 0;
147
		while( $reviews = $this->queryReviews( $args, $lastPostId, $limit )) {
148
			$types = array_keys( array_flip( array_column( $reviews, 'type' )));
149
			foreach( $types as $type ) {
150
				if( isset( $counts[$type] ))continue;
151
				$counts[$type] = array_fill_keys( range( 0, Rating::MAX_RATING ), 0 );
152
			}
153
			foreach( $reviews as $review ) {
154
				$counts[$review->type][$review->rating]++;
155
			}
156
			$lastPostId = end( $reviews )->ID;
157
		}
158
		return $counts;
159
	}
160
161
	/**
162
	 * @return array
163
	 */
164
	protected function normalize( array $reviewCounts )
165
	{
166
		foreach( $reviewCounts as &$counts ) {
167
			foreach( range( 0, Rating::MAX_RATING ) as $index ) {
168
				if( isset( $counts[$index] ))continue;
169
				$counts[$index] = 0;
170
			}
171
			ksort( $counts );
172
		}
173
		return $reviewCounts;
174
	}
175
176
	/**
177
	 * @param int $lastPostId
178
	 * @param int $limit
179
	 * @return void|array
180
	 */
181
	protected function queryReviews( array $args = [], $lastPostId, $limit )
182
	{
183
		$args = wp_parse_args( $args, array_fill_keys( ['post_id', 'term_id'], '' ));
184
		if( empty( array_filter( $args ))) {
185
			return glsr( SqlQueries::class )->getCounts( $lastPostId, $limit );
186
		}
187
		if( !empty( $args['post_id'] )) {
188
			return glsr( SqlQueries::class )->getPostCounts( $args['post_id'], $lastPostId, $limit );
189
		}
190
		if( !empty( $args['term_id'] )) {
191
			return glsr( SqlQueries::class )->getTermCounts( $args['term_id'], $lastPostId, $limit );
192
		}
193
	}
194
}
195