summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/scheduler/src/Common/Timers.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:30:08 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:30:08 +0000
commit4ce65d59ca91871cfd126497158200a818720bce (patch)
treee277def01fc7eba7dbc21c4a4ae5576e8aa2cf1f /vendor/ipl/scheduler/src/Common/Timers.php
parentInitial commit. (diff)
downloadicinga-php-library-upstream/0.13.1.tar.xz
icinga-php-library-upstream/0.13.1.zip
Adding upstream version 0.13.1.upstream/0.13.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/ipl/scheduler/src/Common/Timers.php')
-rw-r--r--vendor/ipl/scheduler/src/Common/Timers.php60
1 files changed, 60 insertions, 0 deletions
diff --git a/vendor/ipl/scheduler/src/Common/Timers.php b/vendor/ipl/scheduler/src/Common/Timers.php
new file mode 100644
index 0000000..2d0641f
--- /dev/null
+++ b/vendor/ipl/scheduler/src/Common/Timers.php
@@ -0,0 +1,60 @@
+<?php
+
+namespace ipl\Scheduler\Common;
+
+use Ramsey\Uuid\UuidInterface;
+use React\EventLoop\TimerInterface;
+use SplObjectStorage;
+
+trait Timers
+{
+ /** @var SplObjectStorage<UuidInterface, TimerInterface> */
+ protected $timers;
+
+ /**
+ * Set a timer for the given UUID
+ *
+ * **Example Usage:**
+ *
+ * ```php
+ * $timers->attachTimer($uuid, Loop::addTimer($interval, $callback));
+ * ```
+ *
+ * @param UuidInterface $uuid
+ * @param TimerInterface $timer
+ *
+ * @return $this
+ */
+ protected function attachTimer(UuidInterface $uuid, TimerInterface $timer): self
+ {
+ $this->timers->attach($uuid, $timer);
+
+ return $this;
+ }
+
+ /**
+ * Detach and return the timer for the given UUID, if any
+ *
+ * **Example Usage:**
+ *
+ * ```php
+ * Loop::cancelTimer($timers->detachTimer($uuid));
+ * ```
+ *
+ * @param UuidInterface $uuid
+ *
+ * @return ?TimerInterface
+ */
+ protected function detachTimer(UuidInterface $uuid): ?TimerInterface
+ {
+ if (! $this->timers->contains($uuid)) {
+ return null;
+ }
+
+ $timer = $this->timers->offsetGet($uuid);
+
+ $this->timers->detach($uuid);
+
+ return $timer;
+ }
+}