Passed
Push — master ( e9a082...4a454f )
by Paul
03:30
created

ReviewController::onAfterChangeCategory()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 12.4085

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 6
dl 0
loc 14
ccs 4
cts 12
cp 0.3333
crap 12.4085
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database;
7
use GeminiLabs\SiteReviews\Database\CountsManager;
8
use GeminiLabs\SiteReviews\Database\ReviewManager;
9
use GeminiLabs\SiteReviews\Helper;
10
use GeminiLabs\SiteReviews\Review;
11
use WP_Post;
12
13
class ReviewController extends Controller
14
{
15
	/**
16
	 * @param int $postId
17
	 * @param array $terms
18
	 * @param array $termTaxonomyIds
19
	 * @param string $taxonomySlug
20
	 * @param bool $append
21
	 * @param array $oldTermTaxonomyIds
22
	 * @return void
23
	 * @action set_object_terms
24
	 */
25 1
	public function onAfterChangeCategory( $postId, $terms, $termTaxonomyIds, $taxonomySlug, $append, $oldTermTaxonomyIds )
26
	{
27 1
		sort( $termTaxonomyIds );
28 1
		sort( $oldTermTaxonomyIds );
29 1
		if( $termTaxonomyIds === $oldTermTaxonomyIds || !$this->isReviewPostId( $postId ))return;
30
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
31
		$ignoredIds = array_intersect( $oldTermTaxonomyIds, $termTaxonomyIds );
32
		$decreasedIds = array_diff( $oldTermTaxonomyIds, $ignoredIds );
33
		$increasedIds = array_diff( $termTaxonomyIds, $ignoredIds );
34
		if( $review->term_ids = glsr( Database::class )->getTermIds( $decreasedIds, 'term_taxonomy_id' )) {
35
			glsr( CountsManager::class )->decreaseTermCounts( $review );
36
		}
37
		if( $review->term_ids = glsr( Database::class )->getTermIds( $increasedIds, 'term_taxonomy_id' )) {
38
			glsr( CountsManager::class )->increaseTermCounts( $review );
39
		}
40
	}
41
42
	/**
43
	 * @param string $oldStatus
44
	 * @param string $newStatus
45
	 * @return void
46
	 * @action transition_post_status
47
	 */
48 1
	public function onAfterChangeStatus( $newStatus, $oldStatus, WP_Post $post )
49
	{
50 1
		if( $post->post_type != Application::POST_TYPE || in_array( $oldStatus, ['new', $newStatus] ))return;
51
		$review = glsr( ReviewManager::class )->single( get_post( $post->ID ));
52
		if( $post->post_status == 'publish' ) {
53
			glsr( CountsManager::class )->increase( $review );
54
		}
55
		else {
56
			glsr( CountsManager::class )->decrease( $review );
57
		}
58
	}
59
60
	/**
61
	 * @return void
62
	 * @action site-reviews/review/created
63
	 */
64 1
	public function onAfterCreate( Review $review )
65
	{
66 1
		if( $review->status !== 'publish' )return;
67 1
		glsr( CountsManager::class )->increase( $review );
68 1
	}
69
70
	/**
71
	 * @param int $postId
72
	 * @return void
73
	 * @action before_delete_post
74
	 */
75
	public function onBeforeDelete( $postId )
76
	{
77
		if( !$this->isReviewPostId( $postId ))return;
78
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
79
		glsr( CountsManager::class )->decrease( $review );
80
	}
81
82
	/**
83
	 * @param int $metaId
84
	 * @param int $postId
85
	 * @param string $metaKey
86
	 * @param mixed $metaValue
87
	 * @return void
88
	 * @action update_postmeta
89
	 */
90
	public function onBeforeUpdate( $metaId, $postId, $metaKey, $metaValue )
91
	{
92
		if( !$this->isReviewPostId( $postId )
93
			|| !in_array( $metaKey, ['assigned_to', 'rating', 'review_type'] )
94
		)return;
95
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
96
		if( $review->$metaKey == $metaValue )return;
97
		$method = glsr( Helper::class )->buildMethodName( $metaKey, 'onBeforeChange' );
98
		call_user_func( [$this, $method], $review, $metaValue );
99
	}
100
101
	/**
102
	 * @param string|int $assignedTo
103
	 * @return void
104
	 */
105
	public function onBeforeChangeAssignedTo( Review $review, $assignedTo )
106
	{
107
		glsr( CountsManager::class )->decreasePostCounts( $review );
108
		$review->assigned_to = $assignedTo;
109
		glsr( CountsManager::class )->increasePostCounts( $review );
110
	}
111
112
	/**
113
	 * @param string|int $rating
114
	 * @return void
115
	 */
116
	public function onBeforeChangeRating( Review $review, $rating )
117
	{
118
		glsr( CountsManager::class )->decrease( $review );
119
		$review->rating = $rating;
120
		glsr( CountsManager::class )->increase( $review );
121
	}
122
123
	/**
124
	 * @param string $reviewType
125
	 * @return void
126
	 */
127
	public function onBeforeChangeReviewType( Review $review, $reviewType )
128
	{
129
		glsr( CountsManager::class )->decrease( $review );
130
		$review->review_type = $reviewType;
131
		glsr( CountsManager::class )->increase( $review );
132
	}
133
}
134