Completed
Push — master ( c21606...af0a2b )
by Naveen
01:12
created
src/wordlift/vocabulary/class-analysis-background-process.php 2 patches
Indentation   +190 added lines, -190 removed lines patch added patch discarded remove patch
@@ -4,198 +4,198 @@
 block discarded – undo
4 4
 
5 5
 class Analysis_Background_Process extends \Wordlift_Plugin_WP_Background_Process {
6 6
 
7
-	const WL_CMKG_ANALYSIS_BACKGROUND_PROCESS = '_wl_cmkg_analysis_background_process';
7
+    const WL_CMKG_ANALYSIS_BACKGROUND_PROCESS = '_wl_cmkg_analysis_background_process';
8 8
 
9 9
 
10
-	protected $action = 'wl_cmkg_analysis_background__analysis';
11
-
12
-	/**
13
-	 * @var Analysis_Background_Service
14
-	 */
15
-	private $analysis_background_service;
16
-
17
-	/**
18
-	 * @var \Wordlift_Log_Service
19
-	 */
20
-	private $log;
10
+    protected $action = 'wl_cmkg_analysis_background__analysis';
11
+
12
+    /**
13
+     * @var Analysis_Background_Service
14
+     */
15
+    private $analysis_background_service;
16
+
17
+    /**
18
+     * @var \Wordlift_Log_Service
19
+     */
20
+    private $log;
21 21
 
22
-	/**
23
-	 * Analysis_Background_Process constructor.
24
-	 *
25
-	 * @param $analysis_background_service Analysis_Background_Service A {@link Analysis_Background_Service} instance providing the supporting functions to this background process.
26
-	 */
27
-	public function __construct( $analysis_background_service ) {
28
-		parent::__construct();
29
-
30
-		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
31
-
32
-		$this->analysis_background_service = $analysis_background_service;
33
-
34
-
35
-	}
36
-
37
-	/**
38
-	 * This function is called:
39
-	 *  - To start a new Synchronization, by passing a {@link Sync_Start_Message} instance.
40
-	 *  - To synchronize a post, by passing a numeric ID.
41
-	 *
42
-	 * This function returns the parameter for the next call or NULL if there are no more posts to process.
43
-	 *
44
-	 * @param int[] $term_ids An array of term IDs.
45
-	 *
46
-	 * @return int[]|false The next term IDs or false if there are no more.
47
-	 */
48
-	protected function task( $term_ids ) {
49
-
50
-		// Check if we must cancel.
51
-		if ( $this->must_cancel() ) {
52
-			$this->cancel();
53
-
54
-			return false;
55
-		}
56
-
57
-		if ( ! $term_ids || ! is_array( $term_ids ) ) {
58
-			$this->cancel();
59
-			return false;
60
-		}
61
-
62
-		$this->log->debug( sprintf( "Synchronizing terms %s...", implode( ', ', $term_ids ) ) );
63
-		// Sync the item.
64
-		return $this->sync_items( $term_ids );
65
-	}
66
-
67
-	/**
68
-	 * Start the background processing.
69
-	 *
70
-	 * @return bool True if the process has been started, otherwise false.
71
-	 */
72
-	public function start() {
73
-		$this->log->debug( "Trying to start analysis bg service..." );
74
-		// Create a new Sync_Model state of `started`.
75
-		if ( ! $this->is_started( self::get_state() ) ) {
76
-			$this->log->debug( "Starting..." );
77
-
78
-			$sync_state = new Sync_State( time(), 0, $this->analysis_background_service->count(), time(), 'started' );
79
-			update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $sync_state, false );
80
-
81
-			$next = $this->analysis_background_service->next();
82
-
83
-			$this->push_to_queue( $next );
84
-			$this->save()->dispatch();
85
-
86
-			if ( $next && is_array( $next ) ) {
87
-				$this->log->debug( sprintf( 'Started with term IDs %s.', implode( ', ', $next ) ) );
88
-			}
89
-
90
-			return true;
91
-		}
92
-
93
-		return false;
94
-	}
95
-
96
-	/**
97
-	 * Set the transient to cancel the process. The next time the process runs, it'll check whether this transient is
98
-	 * set and will stop processing.
99
-	 */
100
-	public function request_cancel() {
101
-
102
-		set_transient( "{$this->action}__cancel", true );
103
-
104
-	}
105
-
106
-	/**
107
-	 * Get the sync state.
108
-	 *
109
-	 * @return Sync_State The {@link Sync_State}.
110
-	 */
111
-	public static function get_state() {
112
-
113
-		try {
114
-			return get_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, Sync_State::unknown() );
115
-		} catch ( \Exception $e ) {
116
-			return Sync_State::unknown();
117
-		}
118
-
119
-	}
120
-
121
-	/**
122
-	 * Check whether the provided state is `started` or not.
123
-	 *
124
-	 * @param Sync_State $state The {@link Sync_State}.
125
-	 *
126
-	 * @return bool True if the state is started.
127
-	 */
128
-	private function is_started( $state ) {
129
-		return $state instanceof Sync_State && 'started' === $state->state && 30 > ( time() - $state->last_update );
130
-	}
131
-
132
-	/**
133
-	 * Check whether the process must cancel or not.
134
-	 *
135
-	 * @return bool Whether to cancel or not the process.
136
-	 */
137
-	private function must_cancel() {
138
-
139
-		return get_transient( "{$this->action}__cancel" );
140
-	}
141
-
142
-	/**
143
-	 * Cancels the current process.
144
-	 */
145
-	public function cancel() {
146
-
147
-		$this->log->debug( "Cancelling synchronization..." );
148
-
149
-		// Cleanup the process data.
150
-		$this->cancel_process();
151
-
152
-		// Set the state to cancelled.
153
-		$state = self::get_state();
154
-		$state->set_state( 'cancelled' );
155
-		update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $state, false );
156
-
157
-		// Finally delete the transient.
158
-		delete_transient( "{$this->action}__cancel" );
159
-
160
-	}
161
-
162
-	/**
163
-	 * Push the post with the provided ID to the remote platform.
164
-	 *
165
-	 * @param int[] $term_ids The term IDs.
166
-	 *
167
-	 * @return int[]|false The next term ID to process or false if processing is complete.
168
-	 */
169
-	private function sync_items( $term_ids ) {
170
-
171
-		// Sync this item.
172
-		if ( $this->analysis_background_service->perform_analysis_for_terms( $term_ids ) ) {
173
-
174
-			$next       = $this->analysis_background_service->next();
175
-			$next_state = isset( $next ) ? 'started' : 'ended';
176
-
177
-			/**
178
-			 * Update the synchronization meta data, by increasing the current index.
179
-			 *
180
-			 * @var Sync_State $sync The {@link Sync_State}.
181
-			 */
182
-			$state = self::get_state()
183
-			             ->increment_index( $this->analysis_background_service->get_batch_size() )
184
-			             ->set_state( $next_state );
185
-			update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS . '', $state, false );
186
-
187
-
188
-			// Return the next IDs or false if there aren't.
189
-			return isset( $next ) ? $next : false;
190
-		} else {
191
-			// Retry.
192
-			// @@todo: put a limit to the number of retries.
193
-
194
-			$this->log->error( sprintf( "Sync failed for terms %s.", implode( ', ', $term_ids ) ) );
195
-
196
-			return $term_ids;
197
-		}
198
-
199
-	}
22
+    /**
23
+     * Analysis_Background_Process constructor.
24
+     *
25
+     * @param $analysis_background_service Analysis_Background_Service A {@link Analysis_Background_Service} instance providing the supporting functions to this background process.
26
+     */
27
+    public function __construct( $analysis_background_service ) {
28
+        parent::__construct();
29
+
30
+        $this->log = \Wordlift_Log_Service::get_logger( get_class() );
31
+
32
+        $this->analysis_background_service = $analysis_background_service;
33
+
34
+
35
+    }
36
+
37
+    /**
38
+     * This function is called:
39
+     *  - To start a new Synchronization, by passing a {@link Sync_Start_Message} instance.
40
+     *  - To synchronize a post, by passing a numeric ID.
41
+     *
42
+     * This function returns the parameter for the next call or NULL if there are no more posts to process.
43
+     *
44
+     * @param int[] $term_ids An array of term IDs.
45
+     *
46
+     * @return int[]|false The next term IDs or false if there are no more.
47
+     */
48
+    protected function task( $term_ids ) {
49
+
50
+        // Check if we must cancel.
51
+        if ( $this->must_cancel() ) {
52
+            $this->cancel();
53
+
54
+            return false;
55
+        }
56
+
57
+        if ( ! $term_ids || ! is_array( $term_ids ) ) {
58
+            $this->cancel();
59
+            return false;
60
+        }
61
+
62
+        $this->log->debug( sprintf( "Synchronizing terms %s...", implode( ', ', $term_ids ) ) );
63
+        // Sync the item.
64
+        return $this->sync_items( $term_ids );
65
+    }
66
+
67
+    /**
68
+     * Start the background processing.
69
+     *
70
+     * @return bool True if the process has been started, otherwise false.
71
+     */
72
+    public function start() {
73
+        $this->log->debug( "Trying to start analysis bg service..." );
74
+        // Create a new Sync_Model state of `started`.
75
+        if ( ! $this->is_started( self::get_state() ) ) {
76
+            $this->log->debug( "Starting..." );
77
+
78
+            $sync_state = new Sync_State( time(), 0, $this->analysis_background_service->count(), time(), 'started' );
79
+            update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $sync_state, false );
80
+
81
+            $next = $this->analysis_background_service->next();
82
+
83
+            $this->push_to_queue( $next );
84
+            $this->save()->dispatch();
85
+
86
+            if ( $next && is_array( $next ) ) {
87
+                $this->log->debug( sprintf( 'Started with term IDs %s.', implode( ', ', $next ) ) );
88
+            }
89
+
90
+            return true;
91
+        }
92
+
93
+        return false;
94
+    }
95
+
96
+    /**
97
+     * Set the transient to cancel the process. The next time the process runs, it'll check whether this transient is
98
+     * set and will stop processing.
99
+     */
100
+    public function request_cancel() {
101
+
102
+        set_transient( "{$this->action}__cancel", true );
103
+
104
+    }
105
+
106
+    /**
107
+     * Get the sync state.
108
+     *
109
+     * @return Sync_State The {@link Sync_State}.
110
+     */
111
+    public static function get_state() {
112
+
113
+        try {
114
+            return get_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, Sync_State::unknown() );
115
+        } catch ( \Exception $e ) {
116
+            return Sync_State::unknown();
117
+        }
118
+
119
+    }
120
+
121
+    /**
122
+     * Check whether the provided state is `started` or not.
123
+     *
124
+     * @param Sync_State $state The {@link Sync_State}.
125
+     *
126
+     * @return bool True if the state is started.
127
+     */
128
+    private function is_started( $state ) {
129
+        return $state instanceof Sync_State && 'started' === $state->state && 30 > ( time() - $state->last_update );
130
+    }
131
+
132
+    /**
133
+     * Check whether the process must cancel or not.
134
+     *
135
+     * @return bool Whether to cancel or not the process.
136
+     */
137
+    private function must_cancel() {
138
+
139
+        return get_transient( "{$this->action}__cancel" );
140
+    }
141
+
142
+    /**
143
+     * Cancels the current process.
144
+     */
145
+    public function cancel() {
146
+
147
+        $this->log->debug( "Cancelling synchronization..." );
148
+
149
+        // Cleanup the process data.
150
+        $this->cancel_process();
151
+
152
+        // Set the state to cancelled.
153
+        $state = self::get_state();
154
+        $state->set_state( 'cancelled' );
155
+        update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $state, false );
156
+
157
+        // Finally delete the transient.
158
+        delete_transient( "{$this->action}__cancel" );
159
+
160
+    }
161
+
162
+    /**
163
+     * Push the post with the provided ID to the remote platform.
164
+     *
165
+     * @param int[] $term_ids The term IDs.
166
+     *
167
+     * @return int[]|false The next term ID to process or false if processing is complete.
168
+     */
169
+    private function sync_items( $term_ids ) {
170
+
171
+        // Sync this item.
172
+        if ( $this->analysis_background_service->perform_analysis_for_terms( $term_ids ) ) {
173
+
174
+            $next       = $this->analysis_background_service->next();
175
+            $next_state = isset( $next ) ? 'started' : 'ended';
176
+
177
+            /**
178
+             * Update the synchronization meta data, by increasing the current index.
179
+             *
180
+             * @var Sync_State $sync The {@link Sync_State}.
181
+             */
182
+            $state = self::get_state()
183
+                            ->increment_index( $this->analysis_background_service->get_batch_size() )
184
+                            ->set_state( $next_state );
185
+            update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS . '', $state, false );
186
+
187
+
188
+            // Return the next IDs or false if there aren't.
189
+            return isset( $next ) ? $next : false;
190
+        } else {
191
+            // Retry.
192
+            // @@todo: put a limit to the number of retries.
193
+
194
+            $this->log->error( sprintf( "Sync failed for terms %s.", implode( ', ', $term_ids ) ) );
195
+
196
+            return $term_ids;
197
+        }
198
+
199
+    }
200 200
 
201 201
 }
Please login to merge, or discard this patch.
Spacing   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -24,10 +24,10 @@  discard block
 block discarded – undo
24 24
 	 *
25 25
 	 * @param $analysis_background_service Analysis_Background_Service A {@link Analysis_Background_Service} instance providing the supporting functions to this background process.
26 26
 	 */
27
-	public function __construct( $analysis_background_service ) {
27
+	public function __construct($analysis_background_service) {
28 28
 		parent::__construct();
29 29
 
30
-		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
30
+		$this->log = \Wordlift_Log_Service::get_logger(get_class());
31 31
 
32 32
 		$this->analysis_background_service = $analysis_background_service;
33 33
 
@@ -45,23 +45,23 @@  discard block
 block discarded – undo
45 45
 	 *
46 46
 	 * @return int[]|false The next term IDs or false if there are no more.
47 47
 	 */
48
-	protected function task( $term_ids ) {
48
+	protected function task($term_ids) {
49 49
 
50 50
 		// Check if we must cancel.
51
-		if ( $this->must_cancel() ) {
51
+		if ($this->must_cancel()) {
52 52
 			$this->cancel();
53 53
 
54 54
 			return false;
55 55
 		}
56 56
 
57
-		if ( ! $term_ids || ! is_array( $term_ids ) ) {
57
+		if ( ! $term_ids || ! is_array($term_ids)) {
58 58
 			$this->cancel();
59 59
 			return false;
60 60
 		}
61 61
 
62
-		$this->log->debug( sprintf( "Synchronizing terms %s...", implode( ', ', $term_ids ) ) );
62
+		$this->log->debug(sprintf("Synchronizing terms %s...", implode(', ', $term_ids)));
63 63
 		// Sync the item.
64
-		return $this->sync_items( $term_ids );
64
+		return $this->sync_items($term_ids);
65 65
 	}
66 66
 
67 67
 	/**
@@ -70,21 +70,21 @@  discard block
 block discarded – undo
70 70
 	 * @return bool True if the process has been started, otherwise false.
71 71
 	 */
72 72
 	public function start() {
73
-		$this->log->debug( "Trying to start analysis bg service..." );
73
+		$this->log->debug("Trying to start analysis bg service...");
74 74
 		// Create a new Sync_Model state of `started`.
75
-		if ( ! $this->is_started( self::get_state() ) ) {
76
-			$this->log->debug( "Starting..." );
75
+		if ( ! $this->is_started(self::get_state())) {
76
+			$this->log->debug("Starting...");
77 77
 
78
-			$sync_state = new Sync_State( time(), 0, $this->analysis_background_service->count(), time(), 'started' );
79
-			update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $sync_state, false );
78
+			$sync_state = new Sync_State(time(), 0, $this->analysis_background_service->count(), time(), 'started');
79
+			update_option(self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $sync_state, false);
80 80
 
81 81
 			$next = $this->analysis_background_service->next();
82 82
 
83
-			$this->push_to_queue( $next );
83
+			$this->push_to_queue($next);
84 84
 			$this->save()->dispatch();
85 85
 
86
-			if ( $next && is_array( $next ) ) {
87
-				$this->log->debug( sprintf( 'Started with term IDs %s.', implode( ', ', $next ) ) );
86
+			if ($next && is_array($next)) {
87
+				$this->log->debug(sprintf('Started with term IDs %s.', implode(', ', $next)));
88 88
 			}
89 89
 
90 90
 			return true;
@@ -99,7 +99,7 @@  discard block
 block discarded – undo
99 99
 	 */
100 100
 	public function request_cancel() {
101 101
 
102
-		set_transient( "{$this->action}__cancel", true );
102
+		set_transient("{$this->action}__cancel", true);
103 103
 
104 104
 	}
105 105
 
@@ -111,8 +111,8 @@  discard block
 block discarded – undo
111 111
 	public static function get_state() {
112 112
 
113 113
 		try {
114
-			return get_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, Sync_State::unknown() );
115
-		} catch ( \Exception $e ) {
114
+			return get_option(self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, Sync_State::unknown());
115
+		} catch (\Exception $e) {
116 116
 			return Sync_State::unknown();
117 117
 		}
118 118
 
@@ -125,8 +125,8 @@  discard block
 block discarded – undo
125 125
 	 *
126 126
 	 * @return bool True if the state is started.
127 127
 	 */
128
-	private function is_started( $state ) {
129
-		return $state instanceof Sync_State && 'started' === $state->state && 30 > ( time() - $state->last_update );
128
+	private function is_started($state) {
129
+		return $state instanceof Sync_State && 'started' === $state->state && 30 > (time() - $state->last_update);
130 130
 	}
131 131
 
132 132
 	/**
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
 	 */
137 137
 	private function must_cancel() {
138 138
 
139
-		return get_transient( "{$this->action}__cancel" );
139
+		return get_transient("{$this->action}__cancel");
140 140
 	}
141 141
 
142 142
 	/**
@@ -144,18 +144,18 @@  discard block
 block discarded – undo
144 144
 	 */
145 145
 	public function cancel() {
146 146
 
147
-		$this->log->debug( "Cancelling synchronization..." );
147
+		$this->log->debug("Cancelling synchronization...");
148 148
 
149 149
 		// Cleanup the process data.
150 150
 		$this->cancel_process();
151 151
 
152 152
 		// Set the state to cancelled.
153 153
 		$state = self::get_state();
154
-		$state->set_state( 'cancelled' );
155
-		update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $state, false );
154
+		$state->set_state('cancelled');
155
+		update_option(self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $state, false);
156 156
 
157 157
 		// Finally delete the transient.
158
-		delete_transient( "{$this->action}__cancel" );
158
+		delete_transient("{$this->action}__cancel");
159 159
 
160 160
 	}
161 161
 
@@ -166,13 +166,13 @@  discard block
 block discarded – undo
166 166
 	 *
167 167
 	 * @return int[]|false The next term ID to process or false if processing is complete.
168 168
 	 */
169
-	private function sync_items( $term_ids ) {
169
+	private function sync_items($term_ids) {
170 170
 
171 171
 		// Sync this item.
172
-		if ( $this->analysis_background_service->perform_analysis_for_terms( $term_ids ) ) {
172
+		if ($this->analysis_background_service->perform_analysis_for_terms($term_ids)) {
173 173
 
174 174
 			$next       = $this->analysis_background_service->next();
175
-			$next_state = isset( $next ) ? 'started' : 'ended';
175
+			$next_state = isset($next) ? 'started' : 'ended';
176 176
 
177 177
 			/**
178 178
 			 * Update the synchronization meta data, by increasing the current index.
@@ -180,18 +180,18 @@  discard block
 block discarded – undo
180 180
 			 * @var Sync_State $sync The {@link Sync_State}.
181 181
 			 */
182 182
 			$state = self::get_state()
183
-			             ->increment_index( $this->analysis_background_service->get_batch_size() )
184
-			             ->set_state( $next_state );
185
-			update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS . '', $state, false );
183
+			             ->increment_index($this->analysis_background_service->get_batch_size())
184
+			             ->set_state($next_state);
185
+			update_option(self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS.'', $state, false);
186 186
 
187 187
 
188 188
 			// Return the next IDs or false if there aren't.
189
-			return isset( $next ) ? $next : false;
189
+			return isset($next) ? $next : false;
190 190
 		} else {
191 191
 			// Retry.
192 192
 			// @@todo: put a limit to the number of retries.
193 193
 
194
-			$this->log->error( sprintf( "Sync failed for terms %s.", implode( ', ', $term_ids ) ) );
194
+			$this->log->error(sprintf("Sync failed for terms %s.", implode(', ', $term_ids)));
195 195
 
196 196
 			return $term_ids;
197 197
 		}
Please login to merge, or discard this patch.