Completed
Pull Request — develop (#1350)
by Naveen
02:56
created

Background_Process::update_batch_index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Common\Background_Process;
4
5
abstract class Background_Process extends \Wordlift_Plugin_WP_Background_Process {
6
7
8
	/**
9
	 * @var \Wordlift_Log_Service
10
	 */
11
	private $log;
12
13
14
	public function __construct( $analysis_background_service ) {
0 ignored issues
show
Unused Code introduced by
The parameter $analysis_background_service is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
15
		parent::__construct();
16
		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
17
	}
18
19
	/**
20
	 * The key which is used to store the Sync_State class for the current process
21
	 * @return string
22
	 */
23
	protected abstract function get_state_storage_key();
24
25
	/**
26
	 * The key which is used as prefix to store the options.
27
	 * @return string
28
	 */
29
	protected abstract function get_action_key();
30
31
32
	/**
33
	 * Check whether the process must cancel or not.
34
	 *
35
	 * @return bool Whether to cancel or not the process.
36
	 */
37
	private function must_cancel() {
38
39
		return get_transient( "{$this->action}__cancel" );
40
	}
41
42
	/**
43
	 * Cancels the current process.
44
	 */
45
	public function cancel() {
46
47
		$action = $this->action;
48
		$this->log->debug( "Cancelling ${action}..." );
49
50
		// Cleanup the process data.
51
		$this->cancel_process();
52
53
		// Set the state to cancelled.
54
		$state = $this->get_state();
55
		$state->set_state( 'cancelled' );
56
		update_option( $this->get_state_storage_key(), $state, false );
57
58
		// Finally delete the transient.
59
		delete_transient( "{$this->action}__cancel" );
60
61
	}
62
63
64
	/**
65
	 * Get the sync state.
66
	 *
67
	 * @return Sync_State The {@link Sync_State}.
68
	 */
69
	public function get_state() {
70
71
		try {
72
			return get_option( $this->get_state_storage_key(), Sync_State::unknown() );
73
		} catch ( \Exception $e ) {
74
			return Sync_State::unknown();
75
		}
76
77
	}
78
79
80
	/**
81
	 * This function is called:
82
	 *  - To start a new Synchronization, by passing a {@link Sync_Start_Message} instance.
83
	 *  - To process a item, by passing a numeric ID.
84
	 *
85
	 * This function returns the parameter for the next call or NULL if there are no more items to process.
86
	 *
87
	 * @param int[] $items An array of term IDs.
0 ignored issues
show
Bug introduced by
There is no parameter named $items. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
88
	 *
89
	 * @return int[]|false The next IDs or false if there are no more.
90
	 */
91
	protected function task( $term_ids ) {
92
93
		// Check if we must cancel.
94
		if ( $this->must_cancel() ) {
95
			$this->cancel();
96
97
			return false;
98
		}
99
100
		if ( $term_ids && is_array( $term_ids ) ) {
101
			$this->log->debug( sprintf( "Synchronizing terms %s...", implode( ', ', $term_ids ) ) );
102
		}
103
		// Sync the item.
104
		$this->process_items( $term_ids );
105
106
		// Update the process state, set the index in state in order
107
		// to reflect the new items.
108
		$this->update_batch_index();
0 ignored issues
show
Unused Code introduced by
The call to the method Wordlift\Common\Backgrou...s::update_batch_index() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
109
110
		// Get the next batch for processing.
111
		return $this->get_next_batch();
112
	}
113
114
	/**
115
	 * @param $items
116
	 *
117
	 * @return void
118
	 */
119
	abstract protected function process_items( $items );
120
121
	/**
122
	 * Return next batch of items after processing.
123
	 * @return int[] or false
124
	 */
125
	abstract protected function get_next_batch();
126
127
	private function update_batch_index() {
128
	}
129
130
131
}