Issues (3)

src/Module/Perique_Queue.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Bootstrap the Queue and Listeners to Perique as it boots.
7
 *
8
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
 *
20
 * @author Glynn Quelch <[email protected]>
21
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
22
 * @package PinkCrab\Queue
23
 * @since 2.0.0
24
 */
25
26
namespace PinkCrab\Queue\Module;
27
28
use PinkCrab\Loader\Hook_Loader;
29
use PinkCrab\Queue\Listener\Listener;
30
use PinkCrab\Queue\Queue_Driver\Queue;
31
use PinkCrab\Perique\Interfaces\Module;
32
use PinkCrab\Perique\Application\App_Config;
33
use PinkCrab\Perique\Interfaces\DI_Container;
34
use PinkCrab\Queue\Queue_Driver\Action_Scheduler\Action_Scheduler_Driver;
35
36
final class Perique_Queue implements Module {
37
38
	private ?Queue $queue_driver = null;
39
40
	/**
41
	 * Set the Queue Driver to use.
42
	 *
43
	 * @param Queue $queue_driver
44
	 * @return self
45
	 */
46
	public function set_queue_driver( Queue $queue_driver ): self {
47
		$this->queue_driver = $queue_driver;
48
49
		// Init the queue driver.
50
		add_action( 'init', array( $this, 'init_driver' ), -1 );
51
52
		return $this;
53
	}
54
55
	/**
56
	 * Gets the queue driver.
57
	 *
58
	 * @return ?Queue
59
	 */
60
	public function get_queue_driver(): ?Queue {
61
		return $this->queue_driver;
62
	}
63
64
	/**
65
	 * Initialise the driver.
66
	 *
67
	 * @return void
68
	 */
69
	public function init_driver(): void {
70
		// If the queue driver is not set, default to the Action Scheduler Driver.
71
		$this->verify_queue_driver();
72
73
		$this->queue_driver->init(); // @phpstan-ignore-line, we have verified the driver is set.
0 ignored issues
show
The method init() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
		$this->queue_driver->/** @scrutinizer ignore-call */ 
74
                       init(); // @phpstan-ignore-line, we have verified the driver is set.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
	}
75
76
	/**
77
	 * Get the Queue Driver to use.
78
	 *
79
	 * Will default to the Action Scheduler Queue Driver, if not defined.
80
	 *
81
	 * @return void
82
	 */
83
	private function verify_queue_driver(): void {
84
		// If the queue driver is not set, default to the Action Scheduler Driver.
85
		if ( ! $this->queue_driver instanceof Queue ) {
86
			$this->queue_driver = Action_Scheduler_Driver::get_instance();
87
		}
88
	}
89
90
	/**
91
	 * Used to create the controller instance and register the hook call, to trigger.
92
	 *
93
	 * @pram App_Config $config
94
	 * @pram Hook_Loader $loader
95
	 * @pram DI_Container $container
96
	 * @return void
97
	 */
98
	public function pre_boot( App_Config $config, Hook_Loader $loader, DI_Container $container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed, Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed
99
100
		$this->verify_queue_driver();
101
102
		// Init queue setup
103
		$this->queue_driver->setup(); // @phpstan-ignore-line, we have verified the driver is set.
104
105
		// Set the DI Rule for the Queue Driver.
106
		$container->addRule(
107
			'*',
108
			array(
109
				'substitutions' => array( Queue::class => $this->queue_driver ),
110
			)
111
		);
112
113
		// Pass the hook loader into any listeners.
114
		$container->addRule(
115
			Listener::class,
116
			array(
117
				'call' => array(
118
					array( 'register', array( $loader ) ),
119
				),
120
			)
121
		);
122
	}
123
124
	/** @inheritDoc */
125
	public function pre_register( App_Config $config, Hook_Loader $loader, DI_Container $container ): void {} // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
126
127
	/** @inheritDoc */
128
	public function post_register( App_Config $config, Hook_Loader $loader, DI_Container $container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed, Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed
129
		// If the queue driver is not set, default to the Action Scheduler Driver.
130
		$this->verify_queue_driver();
131
132
		// Run the queue tear down.
133
		$this->queue_driver->teardown(); // @phpstan-ignore-line, we have verified the driver is set.
134
	}
135
136
	/** @inheritDoc */
137
	public function get_middleware(): ?string {
138
		return null;
139
	}
140
}
141