Passed
Pull Request — master (#15)
by Glynn
10:49 queued 03:43
created

Queue::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Proxy Class for accessing the queue.
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\Dispatch;
27
28
use DateTimeImmutable;
29
use PinkCrab\Queue\Event\Event;
30
use PinkCrab\Perique\Application\App;
31
32
final class Queue {
33
34
	private static ?Queue_Service $instance = null;
35
36
	/** Gets the queue service
37
	 * @return Queue_Service
38
	 */
39
	public static function service(): Queue_Service {
40
		if ( self::$instance === null ) {
41
			/** @var Queue_Service|null */
42
			$service        = App::make( Queue_Service::class );
43
			self::$instance = $service;
44
		}
45
46
		// If we  still dont have an instance, throw an exception.
47
		if ( self::$instance === null ) {
48
			return self::service();
49
		}
50
51
		return self::$instance;
52
	}
53
54
	/**
55
	 * Dispatch Event
56
	 *
57
	 * @param Event $event
58
	 * @return int|null
59
	 */
60
	public static function dispatch( Event $event ): ?int {
61
		return self::service()->dispatch( $event );
62
	}
63
64
	/**
65
	 * Cancel the next instnace of an event.
66
	 *
67
	 * @param Event $event
68
	 * @return void
69
	 */
70
	public static function cancel_next( Event $event ): void {
71
		self::service()->cancel_next( $event );
72
	}
73
74
	/**
75
	 * Cancel all instances of an event.
76
	 *
77
	 * @param Event $event
78
	 * @return void
79
	 */
80
	public static function cancel_all( Event $event ): void {
81
		self::service()->cancel_all( $event );
82
	}
83
84
	/**
85
	 * Get the next instance of an event.
86
	 *
87
	 * @param Event $event
88
	 * @return ?DateTimeImmutable
89
	 */
90
	public static function next( Event $event ): ?DateTimeImmutable {
91
		return self::service()->next( $event );
92
	}
93
94
	/**
95
	 * Get all instances of an event.
96
	 *
97
	 * @param Event $event
98
	 * @return array<int, DateTimeImmutable>
99
	 */
100
	public static function all( Event $event ): array {
101
		return self::service()->all( $event );
102
	}
103
104
	/**
105
	 * Checks if an event exists.
106
	 *
107
	 * @param Event $event
108
	 * @return bool
109
	 */
110
	public static function exists( Event $event ): bool {
111
		return self::service()->exists( $event );
112
	}
113
114
}
115