Passed
Push — master ( 6f6d9e...e32a3c )
by Paul
04:05
created

ReviewManager::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2.0054

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 2
nop 1
dl 0
loc 23
ccs 16
cts 18
cp 0.8889
crap 2.0054
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Commands\CreateReview;
7
use GeminiLabs\SiteReviews\Database\DefaultsManager;
8
use GeminiLabs\SiteReviews\Database\QueryBuilder;
9
use GeminiLabs\SiteReviews\Database\SqlQueries;
10
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults;
11
use GeminiLabs\SiteReviews\Defaults\GetReviewsDefaults;
12
use GeminiLabs\SiteReviews\Helper;
13
use GeminiLabs\SiteReviews\Review;
14
15
class ReviewManager
16
{
17
	/**
18
	 * @return int
19
	 */
20 1
	public function create( CreateReview $command )
21
	{
22 1
		$review = glsr( CreateReviewDefaults::class )->restrict( (array)$command );
23 1
		$review = apply_filters( 'site-reviews/create/review-values', $review, $command );
24
		$post = [
25 1
			'comment_status' => 'closed',
26 1
			'meta_input' => $review,
27 1
			'ping_status' => 'closed',
28 1
			'post_content' => $review['content'],
29 1
			'post_date' => $review['date'],
30 1
			'post_name' => $review['review_type'].'-'.$review['review_id'],
31 1
			'post_status' => $this->getNewPostStatus( $review, $command->blacklisted ),
32 1
			'post_title' => $review['title'],
33
			'post_type' => Application::POST_TYPE,
34
		];
35 1
		$postId = wp_insert_post( $post, true );
36 1
		if( is_wp_error( $postId )) {
37
			glsr_log()->error( $postId->get_error_message() );
38
			return 0;
39
		}
40 1
		$this->setTerms( $postId, $command->category );
1 ignored issue
show
Bug introduced by
It seems like $postId can also be of type WP_Error; however, parameter $postId of GeminiLabs\SiteReviews\D...viewManager::setTerms() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
		$this->setTerms( /** @scrutinizer ignore-type */ $postId, $command->category );
Loading history...
41 1
		do_action( 'site-reviews/create/review', $post, $review, $postId );
42 1
		return $postId;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $postId also could return the type WP_Error which is incompatible with the documented return type integer.
Loading history...
43
	}
44
45
	/**
46
	 * @param string $metaReviewId
47
	 * @return void
48
	 */
49
	public function delete( $metaReviewId )
50
	{
51
		if( $postId = $this->getPostId( $metaReviewId )) {
52
			wp_delete_post( $postId, true );
53
		}
54
	}
55
56
	/**
57
	 * @return object
58
	 */
59
	public function get( array $args = [] )
60
	{
61
		$args = glsr( GetReviewsDefaults::class )->restrict( $args );
62
		$metaQuery = glsr( QueryBuilder::class )->buildQuery(
63
			['assigned_to', 'type', 'rating'],
64
			$args
65
		);
66
		$taxQuery = glsr( QueryBuilder::class )->buildQuery(
67
			['category'],
68
			['category' => $this->normalizeTerms( $args['category'] )]
69
		);
70
		$paged = glsr( QueryBuilder::class )->getPaged(
71
			wp_validate_boolean( $args['pagination'] )
72
		);
73
		$reviews = new WP_Query([
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\Database\WP_Query was not found. Did you mean WP_Query? If so, make sure to prefix the type with \.
Loading history...
74
			'meta_key' => 'pinned',
75
			'meta_query' => $metaQuery,
76
			'offset' => $args['offset'],
77
			'order' => $args['order'],
78
			'orderby' => 'meta_value '.$args['orderby'],
79
			'paged' => $paged,
80
			'post__in' => $args['post__in'],
81
			'post__not_in' => $args['post__not_in'],
82
			'post_status' => 'publish',
83
			'post_type' => Application::POST_TYPE,
84
			'posts_per_page' => $args['count'],
85
			'tax_query' => $taxQuery,
86
		]);
87
		return (object)[
88
			'results' => array_map( [$this, 'single'], $reviews->posts ),
89
			'max_num_pages' => $reviews->max_num_pages,
90
		];
91
	}
92
93
	/**
94
	 * @param string $metaReviewId
95
	 * @return int
96
	 */
97
	public function getPostId( $metaReviewId )
98
	{
99
		return glsr( SqlQueries::class )->getReviewPostId( $metaReviewId );
100
	}
101
102
	/**
103
	 * @param int $postId
104
	 * @return void
105
	 */
106
	public function revert( $postId )
107
	{
108
		if( get_post_field( 'post_type', $postId ) != Application::POST_TYPE )return;
109
		delete_post_meta( $postId, '_edit_last' );
110
		$result = wp_update_post([
111
			'ID' => $postId,
112
			'post_content' => get_post_meta( $postId, 'content', true ),
113
			'post_date' => get_post_meta( $postId, 'date', true ),
114
			'post_title' => get_post_meta( $postId, 'title', true ),
115
		]);
116
		if( is_wp_error( $result )) {
117
			glsr_log()->error( $result->get_error_message() );
118
		}
119
	}
120
121
	/**
122
	 * @return Review
123
	 */
124
	public function single( WP_Post $post )
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\Database\WP_Post was not found. Did you mean WP_Post? If so, make sure to prefix the type with \.
Loading history...
125
	{
126
		if( $post->post_type != Application::POST_TYPE )return;
127
		$review = new Review( $post );
128
		return apply_filters( 'site-reviews/get/review', $review, $post );
129
	}
130
131
	/**
132
	 * @param bool $isBlacklisted
133
	 * @return string
134
	 */
135 1
	protected function getNewPostStatus( array $review, $isBlacklisted )
136
	{
137 1
		$requireApprovalOption = glsr( OptionManager::class )->get( 'settings.general.require.approval' );
138 1
		return $review['review_type'] == 'local' && ( $requireApprovalOption == 'yes' || $isBlacklisted )
139
			? 'pending'
140 1
			: 'publish';
141
	}
142
143
	/**
144
	 * @param string $commaSeparatedTermIds
145
	 * @return array
146
	 */
147 1
	protected function normalizeTerms( $commaSeparatedTermIds )
148
	{
149 1
		$terms = [];
150 1
		$termIds = array_map( 'trim', explode( ',', $commaSeparatedTermIds ));
151 1
		foreach( $termIds as $termId ) {
152 1
			$term = term_exists( $termId, Application::TAXONOMY );
153 1
			if( !isset( $term['term_id'] ))continue;
154
			$terms[] = intval( $term['term_id'] );
155
		}
156 1
		return $terms;
157
	}
158
159
	/**
160
	 * @param int $postId
161
	 * @param string $termIds
162
	 * @return void
163
	 */
164 1
	protected function setTerms( $postId, $termIds )
165
	{
166 1
		$terms = $this->normalizeTerms( $termIds );
167 1
		if( empty( $terms ))return;
168
		$result = wp_set_object_terms( $postId, $terms, Application::TAXONOMY );
169
		if( is_wp_error( $result )) {
170
			glsr_log()->error( $result->get_error_message() );
171
		}
172
	}
173
}
174