Test Failed
Push — master ( 8dc102...1ef8c4 )
by Paul
03:41
created

CountsManager::decrease()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
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
	 * @param int $limit
115
	 * @return array
116
	 */
117
	protected function build( $limit, array $args = [] )
118
	{
119
		$counts = [];
120
		$lastPostId = 0;
121
		while( $reviews = $this->queryReviews( $args, $lastPostId, $limit )) {
122
			$types = array_keys( array_flip( array_column( $reviews, 'type' )));
123
			foreach( $types as $type ) {
124
				if( isset( $counts[$type] ))continue;
125
				$counts[$type] = array_fill_keys( range( 0, Rating::MAX_RATING ), 0 );
126
			}
127
			foreach( $reviews as $review ) {
128
				$counts[$review->type][$review->rating]++;
129
			}
130
			$lastPostId = end( $reviews )->ID;
131
		}
132
		return $counts;
133
	}
134
135
	/**
136
	 * @return array
137
	 */
138
	protected function normalize( array $reviewCounts )
139
	{
140
		foreach( $reviewCounts as &$counts ) {
141
			foreach( range( 0, Rating::MAX_RATING ) as $index ) {
142
				if( isset( $counts[$index] ))continue;
143
				$counts[$index] = 0;
144
			}
145
			ksort( $counts );
146
		}
147
		return $reviewCounts;
148
	}
149
150
	/**
151
	 * @param int $lastPostId
152
	 * @param int $limit
153
	 * @return void|array
154
	 */
155
	protected function queryReviews( array $args = [], $lastPostId, $limit )
156
	{
157
		$args = wp_parse_args( $args, array_fill_keys( ['post_id', 'term_id'], '' ));
158
		if( empty( array_filter( $args ))) {
159
			return glsr( SqlQueries::class )->getCounts( $lastPostId, $limit );
160
		}
161
		if( !empty( $args['post_id'] )) {
162
			return glsr( SqlQueries::class )->getPostCounts( $args['post_id'], $lastPostId, $limit );
163
		}
164
		if( !empty( $args['term_id'] )) {
165
			return glsr( SqlQueries::class )->getTermCounts( $args['term_id'], $lastPostId, $limit );
166
		}
167
	}
168
}
169