Passed
Push — master ( ecb0f6...008868 )
by Paul
04:35
created

ReviewController::onAfterCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
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 5
ccs 0
cts 4
cp 0
crap 6
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 array $postData
41
	 * @param array $meta
42
	 * @param int $postId
43
	 * @return void
44
	 * @action site-reviews/create/review
45
	 */
46
	public function onAfterCreate( $postData, $meta, $postId )
47
	{
48
		if( !$this->isReviewPostId( $postId ))return;
49
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
50
		glsr( CountsManager::class )->increase( $review );
51
	}
52
53
	/**
54
	 * @param int $postId
55
	 * @return void
56
	 * @action before_delete_post
57
	 */
58
	public function onBeforeDelete( $postId )
59
	{
60
		if( !$this->isReviewPostId( $postId ))return;
61
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
62
		glsr( CountsManager::class )->decrease( $review );
63
	}
64
65
	/**
66
	 * @param int $metaId
67
	 * @param int $postId
68
	 * @param string $metaKey
69
	 * @param mixed $metaValue
70
	 * @return void
71
	 * @action update_postmeta
72
	 */
73
	public function onBeforeUpdate( $metaId, $postId, $metaKey, $metaValue )
74
	{
75
		if( !$this->isReviewPostId( $postId )
76
			|| !in_array( $metaKey, ['assigned_to', 'rating', 'review_type'] )
77
		)return;
78
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
79
		if( $review->$metaKey == $metaValue )return;
80
		$method = glsr( Helper::class )->buildMethodName( $metaKey, 'onBeforeChange' );
81
		call_user_func( [$this, $method], $review, $metaValue );
82
	}
83
84
	/**
85
	 * @param string|int $assignedTo
86
	 * @return void
87
	 */
88
	public function onBeforeChangeAssignedTo( Review $review, $assignedTo )
89
	{
90
		glsr( CountsManager::class )->decreasePostCounts( $review );
91
		$review->assigned_to = $assignedTo;
92
		glsr( CountsManager::class )->increasePostCounts( $review );
93
	}
94
95
	/**
96
	 * @param string|int $rating
97
	 * @return void
98
	 */
99
	public function onBeforeChangeRating( Review $review, $rating )
100
	{
101
		glsr( CountsManager::class )->decrease( $review );
102
		$review->rating = $rating;
103
		glsr( CountsManager::class )->increase( $review );
104
	}
105
106
	/**
107
	 * @param string $reviewType
108
	 * @return void
109
	 */
110
	public function onBeforeChangeReviewType( Review $review, $reviewType )
111
	{
112
		glsr( CountsManager::class )->decrease( $review );
113
		$review->review_type = $reviewType;
114
		glsr( CountsManager::class )->increase( $review );
115
	}
116
117
	/**
118
	 * @param string $oldStatus
119
	 * @param string $newStatus
120
	 * @return void
121
	 * @action transition_post_status
122
	 */
123 1
	public function onChangeStatus( $newStatus, $oldStatus, WP_Post $post )
124
	{
125 1
		if( $post->post_type != Application::POST_TYPE || in_array( $oldStatus, ['new', $newStatus] ))return;
126
		$review = glsr( ReviewManager::class )->single( get_post( $post->ID ));
127
		if( $post->post_status == 'publish' ) {
128
			glsr( CountsManager::class )->increase( $review );
129
		}
130
		else {
131
			glsr( CountsManager::class )->decrease( $review );
132
		}
133
	}
134
}
135