Completed
Push — develop ( 88fa56...72a91d )
by
unknown
39s queued 12s
created

Sync_Background_Process_Started_State::resume()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Dataset\Background;
4
5
use Wordlift\Dataset\Background\Stages\Sync_Background_Process_Posts_Stage;
6
use Wordlift\Dataset\Background\Stages\Sync_Background_Process_Stage;
7
use Wordlift\Dataset\Background\Stages\Sync_Background_Process_Terms_Stage;
8
use Wordlift\Dataset\Background\Stages\Sync_Background_Process_Users_Stage;
9
use Wordlift\Dataset\Sync_Object_Adapter_Factory;
10
use Wordlift\Dataset\Sync_Service;
11
12
class Sync_Background_Process_Started_State extends Abstract_Sync_Background_Process_State {
13
14
	/**
15
	 * @var Sync_Background_Process
16
	 */
17
	private $context;
18
19
	/**
20
	 * @var Sync_Service
21
	 */
22
	private $sync_service;
23
24
	/**
25
	 * @var Sync_Background_Process_Stage[]
26
	 */
27
	private $stages;
28
29
	private $batch_size = 5;
30
	/**
31
	 * @var bool
32
	 */
33
	private $reset;
34
35
	/**
36
	 * Sync_Background_Process_Started_State constructor.
37
	 *
38
	 * @param Sync_Background_Process $context
39
	 * @param Sync_Service $sync_service
40
	 * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory
41
	 * @param bool $reset Whether to reset the counters
42
	 */
43
	function __construct( $context, $sync_service, $sync_object_adapter_factory, $reset = true ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Unused Code introduced by
The parameter $reset 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...
44
		parent::__construct( Sync_Background_Process::STATE_STARTED );
45
46
		$this->context      = $context;
47
		$this->sync_service = $sync_service;
48
49
		$this->stages = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array(new \Wordlift\Data...bject_adapter_factory)) of type array<integer,object<Wor...Process_Users_Stage>"}> is incompatible with the declared type array<integer,object<Wor...kground_Process_Stage>> of property $stages.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
			new Sync_Background_Process_Posts_Stage( $sync_object_adapter_factory ),
51
			new Sync_Background_Process_Terms_Stage( $sync_object_adapter_factory ),
52
			new Sync_Background_Process_Users_Stage( $sync_object_adapter_factory ),
53
		);
54
	}
55
56
	function enter() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
57
		// Delete the KG contents.
58
		$this->sync_service->delete_all();
59
60
		// Clear caches.
61
		do_action( 'wl_ttl_cache_cleaner__flush' );
62
63
		$counts = array_map( function ( $item ) {
64
			return $item->count();
65
		}, $this->stages );
66
67
		update_option( '_wl_sync_background_process_count', $counts, true );
68
		update_option( '_wl_sync_background_process_stage', 0, true );
69
		update_option( '_wl_sync_background_process_offset', 0, true );
70
		update_option( '_wl_sync_background_process_started', time(), true );
71
		update_option( '_wl_sync_background_process_updated', time(), true );
72
73
		$this->context->set_state( Sync_Background_Process::STATE_STARTED );
74
75
		$this->resume();
76
	}
77
78
	function resume() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
79
		$this->context->push_to_queue( true );
80
		$this->context->save()->dispatch();
81
	}
82
83
	function leave() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
84
		$this->context->set_state( null );
85
	}
86
87
	function task( $args ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
88
89
		$offset     = get_option( '_wl_sync_background_process_offset' );
90
		$stage      = get_option( '_wl_sync_background_process_stage' );
91
		$counts     = get_option( '_wl_sync_background_process_count' );
92
		$batch_size = min( $counts[ $stage ] - $offset, $this->batch_size );
93
94
		add_filter( 'wl_api_service__request', array( $this, 'api_service__request' ) );
95
		try {
96
			$object_adapters = $this->stages[ $stage ]->get_sync_object_adapters( $offset, $batch_size );
97
			$this->sync_service->sync_many( $object_adapters, true );
98
		} catch ( \Exception $e ) {
99
			// ignored.
100
		}
101
		remove_filter( 'wl_api_service__request', array( $this, 'api_service__request' ) );
102
103
		update_option( '_wl_sync_background_process_updated', time(), true );
104
105
		// Increase the offset.
106
		if ( ( $offset + $batch_size ) < $counts[ $stage ] ) {
107
			update_option( '_wl_sync_background_process_offset', $offset + $batch_size, true );
108
109
			return true;
110
		}
111
112
		// Increase the stage.
113
		if ( ( $stage + 1 ) < count( $this->stages ) ) {
114
			update_option( '_wl_sync_background_process_stage', $stage + 1, true );
115
			update_option( '_wl_sync_background_process_offset', 0, true );
116
117
			return true;
118
		}
119
120
		// Stop processing.
121
		$this->context->stop();
122
123
		return false;
124
	}
125
126
	/**
127
	 * Hook to provide a request to update the status on the server.
128
	 *
129
	 * @param array $args
130
	 *
131
	 * @return mixed
132
	 */
133
	public function api_service__request( $args ) {
134
135
		$state_header_value = str_replace( "\n", '', wp_json_encode( $this->context->get_info() ) );
136
137
		$args['headers']['X-Wordlift-Dataset-Sync-State-V1'] = $state_header_value;
138
139
		return $args;
140
	}
141
142
}
143