Completed
Pull Request — develop (#1328)
by Naveen
03:04
created

Analysis_Background_Service   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 135
Duplicated Lines 28.89 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 39
loc 135
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A start() 0 3 1
A cancel() 0 3 1
A stop() 0 4 1
A next() 18 18 1
A count() 21 21 1
A get_batch_size() 0 3 1
A info() 0 3 1
A perform_analysis_for_terms() 0 31 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Wordlift\Vocabulary;
4
5
6
class Analysis_Background_Service {
7
8
9
	const ANALYSIS_DONE_FLAG = '_wl_cmkg_analysis_complete_for_term_options_cache';
10
	const TERMS_COUNT_TRANSIENT = '_wl_cmkg_analysis_background_service_terms_count';
11
	const ENTITIES_PRESENT_FOR_TERM = '_wl_cmkg_analysis_entities_present_for_term_options_cache';
12
13
	/**
14
	 * @var Analysis_Service
15
	 */
16
	private $analysis_service;
17
	/**
18
	 * @var Analysis_Background_Process
19
	 */
20
	private $analysis_background_process;
21
22
	/**
23
	 * @var \Wordlift_Log_Service
24
	 */
25
	private $log;
26
27
28
	public function __construct( $analysis_service ) {
29
30
		$this->analysis_service = $analysis_service;
31
32
		$this->analysis_background_process = new Analysis_Background_Process( $this );
33
34
		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
35
	}
36
37
	public function start() {
38
		$this->analysis_background_process->start();
39
	}
40
41
	public function cancel() {
42
		$this->analysis_background_process->cancel();
43
	}
44
45
	public function stop() {
46
		$this->analysis_background_process->cancel();
47
		$this->analysis_background_process->request_cancel();
48
	}
49
50
	/**
51
	 * A list of term ids.
52
	 * @return int|\WP_Error|\WP_Term[]
53
	 */
54 View Code Duplication
	public function next() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
56
		$state = $this->info();
57
58
		return get_terms( array(
59
			'fields'     => 'ids',
60
			'taxonomy'   => 'post_tag',
61
			'hide_empty' => false,
62
			'number'     => $this->get_batch_size(),
63
			'offset'     => $state->index,
64
			'meta_query' => array(
65
				array(
66
					'key'     => self::ANALYSIS_DONE_FLAG,
67
					'compare' => 'NOT EXISTS'
68
				)
69
			),
70
		) );
71
	}
72
73 View Code Duplication
	public function count() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
75
		$count = count( get_terms( array(
76
			'fields'     => 'ids',
77
			'taxonomy'   => 'post_tag',
78
			'hide_empty' => false,
79
			// return all terms, we cant pass -1 here.
80
			'number'     => 0,
81
			'meta_query' => array(
82
				array(
83
					'key'     => self::ANALYSIS_DONE_FLAG,
84
					'compare' => 'NOT EXISTS'
85
				)
86
			),
87
		) ) );
88
89
		$this->log->debug( "Count returned as $count" );
90
91
92
		return $count;
93
	}
94
95
	public function get_batch_size() {
96
		return 10;
97
	}
98
99
	public function info() {
100
		return Analysis_Background_Process::get_state();
101
	}
102
103
	/**
104
	 * @param $term_ids
105
	 *
106
	 * @return bool
107
	 */
108
	public function perform_analysis_for_terms( $term_ids ) {
109
110
		foreach ( $term_ids as $term_id ) {
111
112
			$tag = get_term( $term_id );
113
114
			// This adds the entities to ttl cache
115
			$result = $this->analysis_service->get_entities( $tag );
116
117
			$this->log->debug( "Received result " . var_export( $result ) . " for ${term_id}" );
118
119
			if ( $result !== false ) {
120
				// then set the analysis complete flag.
121
				update_term_meta( $term_id, self::ANALYSIS_DONE_FLAG, 1 );
122
				if ( count( $result ) > 0 ) {
123
					update_term_meta( $term_id, self::ENTITIES_PRESENT_FOR_TERM, 1 );
124
				}
125
			}
126
127
		}
128
129
		/**
130
		 * This action fires when the analysis is complete for the current batch
131
		 * @since 3.30.0
132
		 */
133
		do_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch' );
134
135
		return true;
136
137
138
	}
139
140
}