summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/scheduler/src/Common/Promises.php
blob: b8966274571c533cbcc2d55d69df0090645329bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php

namespace ipl\Scheduler\Common;

use ArrayObject;
use InvalidArgumentException;
use Ramsey\Uuid\UuidInterface;
use React\Promise\PromiseInterface;
use SplObjectStorage;

trait Promises
{
    /** @var SplObjectStorage<UuidInterface, ArrayObject<int, PromiseInterface>> */
    protected $promises;

    /**
     * Add the given promise for the specified UUID
     *
     * **Example Usage:**
     *
     * ```php
     * $promise = work();
     * $promises->addPromise($uuid, $promise);
     * ```
     *
     * @param UuidInterface $uuid
     * @param PromiseInterface $promise
     *
     * @return $this
     */
    protected function addPromise(UuidInterface $uuid, PromiseInterface $promise): self
    {
        if (! $this->promises->contains($uuid)) {
            $this->promises->attach($uuid, new ArrayObject());
        }

        $this->promises[$uuid][] = $promise;

        return $this;
    }

    /**
     * Remove the given promise for the specified UUID
     *
     * **Example Usage:**
     *
     * ```php
     * $promise->always(function () use ($uuid, $promise) {
     *     $promises->removePromise($uuid, $promise);
     * })
     * ```
     *
     * @param UuidInterface $uuid
     * @param PromiseInterface $promise
     *
     * @return $this
     *
     * @throws InvalidArgumentException If the given UUID doesn't have any registered promises or when the specified
     *                                  UUID promises doesn't contain the provided promise
     */
    protected function removePromise(UuidInterface $uuid, PromiseInterface $promise): self
    {
        if (! $this->promises->contains($uuid)) {
            throw new InvalidArgumentException(
                sprintf('There are no registered promises for UUID %s', $uuid->toString())
            );
        }

        foreach ($this->promises[$uuid] as $k => $v) {
            if ($v === $promise) {
                unset($this->promises[$uuid][$k]);

                return $this;
            }
        }

        throw new InvalidArgumentException(
            sprintf('There is no such promise for UUID %s', $uuid->toString())
        );
    }

    /**
     * Detach and return promises for the given UUID, if any
     *
     * **Example Usage:**
     *
     * ```php
     * foreach ($promises->detachPromises($uuid) as $promise) {
     *     $promise->cancel();
     * }
     * ```
     *
     * @param UuidInterface $uuid
     *
     * @return PromiseInterface[]
     */
    protected function detachPromises(UuidInterface $uuid): array
    {
        if (! $this->promises->contains($uuid)) {
            return [];
        }

        $promises = $this->promises[$uuid];
        $this->promises->detach($uuid);

        return $promises->getArrayCopy();
    }
}