1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Injectable Queue Service for interacting with 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 0.1.0 |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace PinkCrab\Queue\Dispatch; |
27
|
|
|
|
28
|
|
|
use DateTimeImmutable; |
29
|
|
|
use PinkCrab\Queue\Event\Event; |
30
|
|
|
use PinkCrab\Queue\Queue_Driver\Queue; |
|
|
|
|
31
|
|
|
|
32
|
|
|
final class Queue_Service { |
33
|
|
|
|
34
|
|
|
private Queue $queue; |
35
|
|
|
|
36
|
|
|
public function __construct( Queue $queue ) { |
37
|
|
|
$this->queue = $queue; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Dispatch Event |
42
|
|
|
* |
43
|
|
|
* @param Event $event |
44
|
|
|
*/ |
45
|
|
|
public function dispatch( Event $event ): ?int { |
46
|
|
|
return $this->queue->dispatch( $event ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Cancel next occurrence of Event |
51
|
|
|
* |
52
|
|
|
* @param Event $event |
53
|
|
|
*/ |
54
|
|
|
public function cancel_next( Event $event ): void { |
55
|
|
|
$this->queue->cancel_next( $event ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Cancel all occurrences of Event |
60
|
|
|
* |
61
|
|
|
* @param Event $event |
62
|
|
|
*/ |
63
|
|
|
public function cancel_all( Event $event ): void { |
64
|
|
|
$this->queue->cancel_all( $event ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get next occurrence of Event |
69
|
|
|
* |
70
|
|
|
* @param Event $event |
71
|
|
|
* @return DateTimeImmutable|null |
72
|
|
|
*/ |
73
|
|
|
public function next( Event $event ): ?DateTimeImmutable { |
74
|
|
|
return $this->queue->find_next( $event ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get all occurrences of Event |
79
|
|
|
* |
80
|
|
|
* @param Event $event |
81
|
|
|
* @return DateTimeImmutable[] |
82
|
|
|
*/ |
83
|
|
|
public function all( Event $event ): array { |
84
|
|
|
return $this->queue->find_all( $event ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Checks if an event exists. |
90
|
|
|
* |
91
|
|
|
* @param Event $event |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function exists( Event $event ): bool { |
95
|
|
|
return $this->queue->is_scheduled( $event ); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: