Test Failed
Push — master ( 3b4aec...d6370d )
by Paul
03:25
created

ReviewController::isReviewPostType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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
11
class ReviewController extends Controller
12
{
13
	/**
14
	 * @param int $postId
15
	 * @param array $terms
16
	 * @param array $termIds
17
	 * @param string $taxonomySlug
18
	 * @param bool $append
19
	 * @param array $oldTermIds
20
	 * @return void
21
	 * @action set_object_terms
22
	 */
23
	public function onAfterChangeCategory( $postId, $terms, $termIds, $taxonomySlug, $append, $oldTermIds )
24
	{
25
		if( !$this->isReviewPostType( $postId ))return;
26
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
27
		$ignoredTerms = array_intersect( $oldTermIds, $termIds );
28
		$review->term_ids = array_diff( $oldTermIds, $ignoredTerms );
29
		glsr( CountsManager::class )->decreaseTermCounts( $review );
30
		$review->term_ids = array_diff( $termIds, $ignoredTerms );
31
		glsr( CountsManager::class )->increaseTermCounts( $review );
32
	}
33
34
	/**
35
	 * @param array $postData
36
	 * @param array $meta
37
	 * @param int $postId
38
	 * @return void
39
	 * @action site-reviews/create/review
40
	 */
41
	public function onAfterCreate( $postData, $meta, $postId )
42
	{
43
		if( !$this->isReviewPostType( $postId ))return;
44
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
45
		glsr( CountsManager::class )->increase( $review );
46
	}
47
48
	/**
49
	 * @param int $postId
50
	 * @return void
51
	 * @action before_delete_post
52
	 */
53
	public function onBeforeDelete( $postId )
54
	{
55
		if( !$this->isReviewPostType( $postId ))return;
56
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
57
		glsr( CountsManager::class )->decrease( $review );
58
	}
59
60
	/**
61
	 * @param int $metaId
62
	 * @param int $postId
63
	 * @param string $metaKey
64
	 * @param mixed $metaValue
65
	 * @return void
66
	 * @action update_postmeta
67
	 */
68
	public function onBeforeUpdate( $metaId, $postId, $metaKey, $metaValue )
69
	{
70
		if( !$this->isReviewPostType( $postId )
71
			|| !in_array( $metaKey, ['assigned_to', 'rating', 'review_type'] )
72
		)return;
73
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
74
		if( $review->$metaKey == $metaValue )return;
75
		$method = glsr( Helper::class )->buildMethodName( $metaKey, 'onBeforeChange' );
76
		call_user_func( [$this, $method], $review, $metaValue );
77
	}
78
79
	public function onBeforeChangeAssignedTo( Review $review, $metaValue )
80
	{
81
	}
82
83
	public function onBeforeChangeRating( Review $review, $metaValue )
84
	{
85
	}
86
87
	public function onBeforeChangeReviewType( Review $review, $metaValue )
88
	{
89
	}
90
91
	/**
92
	 * @param string $oldStatus
93
	 * @param string $newStatus
94
	 * @return void
95
	 * @action transition_post_status
96
	 */
97
	public function onChangeStatus( $newStatus, $oldStatus, WP_Post $post )
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\Controllers\WP_Post was not found. Did you mean WP_Post? If so, make sure to prefix the type with \.
Loading history...
98
	{
99
		if( $post->post_type != Application::POST_TYPE || in_array( $oldStatus, ['new', $newStatus] ))return;
100
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $postId does not exist. Did you maybe mean $post?
Loading history...
101
		if( $status == 'publish' ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $status seems to be never defined.
Loading history...
102
			glsr( CountsManager::class )->increase( $review );
103
		}
104
		else {
105
			glsr( CountsManager::class )->decrease( $review );
106
		}
107
	}
108
109
	/**
110
	 * @param int $postId
111
	 * @return bool
112
	 */
113
	protected function isReviewPostType( $postId )
114
	{
115
		return get_post_field( 'post_type', $postId ) !== Application::POST_TYPE;
116
	}
117
}
118