summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost/SparkPostPromise.php
blob: b7ded0cb6e0047b5b752dcebc4253f7708bc2513 (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
<?php

namespace SparkPost;

use Http\Promise\Promise as HttpPromise;

class SparkPostPromise implements HttpPromise
{
    /**
     * HttpPromise to be wrapped by SparkPostPromise.
     */
    private $promise;

    /**
     * Array with the request values sent.
     */
    private $request;

    /**
     * set the promise to be wrapped.
     *
     * @param HttpPromise $promise
     */
    public function __construct(HttpPromise $promise, $request = null)
    {
        $this->promise = $promise;
        $this->request = $request;
    }

    /**
     * Hand off the response functions to the original promise and return a custom response or exception.
     *
     * @param callable $onFulfilled - function to be called if the promise is fulfilled
     * @param callable $onRejected  - function to be called if the promise is rejected
     */
    public function then(callable $onFulfilled = null, callable $onRejected = null)
    {
        $request = $this->request;

        return $this->promise->then(function ($response) use ($onFulfilled, $request) {
            if (isset($onFulfilled)) {
                $onFulfilled(new SparkPostResponse($response, $request));
            }
        }, function ($exception) use ($onRejected, $request) {
            if (isset($onRejected)) {
                $onRejected(new SparkPostException($exception, $request));
            }
        });
    }

    /**
     * Hand back the state.
     *
     * @return $state - returns the state of the promise
     */
    public function getState()
    {
        return $this->promise->getState();
    }

    /**
     * Wraps the wait function and returns a custom response or throws a custom exception.
     *
     * @param bool $unwrap
     *
     * @return SparkPostResponse
     *
     * @throws SparkPostException
     */
    public function wait($unwrap = true)
    {
        try {
            $response = $this->promise->wait($unwrap);

            return $response ? new SparkPostResponse($response, $this->request) : $response;
        } catch (\Exception $exception) {
            throw new SparkPostException($exception, $this->request);
        }
    }
}