Passed
Push — master ( 9a7a6b...7de9ff )
by Paul
03:37
created

ReviewController::onAfterChangeStatus()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 8.7414

Importance

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