Completed
Pull Request — develop (#1282)
by David
07:16 queued 04:07
created

Sync_Background_Process::get_info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Dataset\Background;
4
5
use Wordlift\Dataset\Sync_Object_Adapter_Factory;
6
use Wordlift\Dataset\Sync_Service;
7
8
/**
9
 * Class Sync_Background_Process
10
 *
11
 * The background process has the following states:
12
 *  - STOPPING
13
 *  - STOPPED
14
 *  - STARTING
15
 *  - STARTED
16
 *
17
 * @package Wordlift\Dataset\Background
18
 */
19
class Sync_Background_Process extends \Wordlift_Plugin_WP_Background_Process {
20
21
	const STATE_STARTED = 'started';
22
	const STATE_STOPPED = 'stopped';
23
24
	protected $action = 'wl_dataset__sync';
25
26
	/**
27
	 * @var Sync_Service
28
	 */
29
	private $sync_service;
30
31
	/**
32
	 * @var Sync_Object_Adapter_Factory
33
	 */
34
	private $sync_object_adapter_factory;
35
36
	/**
37
	 * @var \Wordlift_Log_Service
38
	 */
39
	private $log;
40
41
	/**
42
	 * @var Sync_Background_Process_State
43
	 */
44
	private $state;
45
46
	/**
47
	 * Sync_Background_Process constructor.
48
	 *
49
	 * @param Sync_Service $sync_service A {@link Sync_Service} instance providing the supporting functions to this background process.
50
	 * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory
51
	 */
52
	public function __construct( $sync_service, $sync_object_adapter_factory ) {
53
		parent::__construct();
54
55
		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
56
57
		$this->sync_service                = $sync_service;
58
		$this->sync_object_adapter_factory = $sync_object_adapter_factory;
59
60
		// Set the current state.
61
		if ( self::STATE_STARTED === $this->get_state() ) {
62
			$this->state = new Sync_Background_Process_Started_State( $this, $this->sync_service, $this->sync_object_adapter_factory );
63
		} else {
64
			$this->state = new Sync_Background_Process_Stopped_State( $this );
65
		}
66
67
	}
68
69
	/**
70
	 * This function is called:
71
	 *  - To start a new Synchronization, by passing a {@link Sync_Start_Message} instance.
72
	 *  - To synchronize a post, by passing a numeric ID.
73
	 *
74
	 * This function returns the parameter for the next call or NULL if there are no more posts to process.
75
	 *
76
	 * @param mixed $item Queue item to iterate over.
77
	 *
78
	 * @return int[]|false The next post IDs or false if there are no more.
79
	 */
80
	protected function task( $item ) {
81
82
		return $this->state->task( $item );
83
	}
84
85
	/**
86
	 * Transition to the started state.
87
	 */
88
	public function start() {
89
		$this->state->leave();
90
		$this->state = new Sync_Background_Process_Started_State( $this, $this->sync_service, $this->sync_object_adapter_factory );
91
		$this->state->enter();
92
	}
93
94
	/**
95
	 * Transition to the stopped state.
96
	 */
97
	public function stop() {
98
		$this->state->leave();
99
		$this->state = new Sync_Background_Process_Stopped_State( $this );
100
		$this->state->enter();
101
	}
102
103
	/**
104
	 * Get the current state.
105
	 *
106
	 * @return string Either self::STARTED_STATE or self::STOPPED_STATE (default).
107
	 */
108
	public function get_state() {
109
		return get_option( '_wl_sync_background_process_state', self::STATE_STOPPED );
110
	}
111
112
	/**
113
	 * Persist the current state.
114
	 *
115
	 * @param string $value
116
	 *
117
	 * @return bool
118
	 */
119
	public function set_state( $value ) {
120
		return null === $value
121
			? delete_option( '_wl_sync_background_process_state' )
122
			: update_option( '_wl_sync_background_process_state', $value, true );
123
	}
124
125
	public function get_info() {
126
		return $this->state->get_info();
127
	}
128
129
}
130