• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

voku / httpful / 5623107679

pending completion
5623107679

push

github

voku
[+]: fix test for the new release

1596 of 2486 relevant lines covered (64.2%)

81.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

42.55
/src/Httpful/Curl/MultiCurlPromise.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Httpful\Curl;
6

7
use Http\Promise\Promise;
8

9
/**
10
 * Promise represents a response that may not be available yet, but will be resolved at some point
11
 * in future. It acts like a proxy to the actual response.
12
 */
13
class MultiCurlPromise implements Promise
14
{
15
    /**
16
     * Requests runner.
17
     *
18
     * @var MultiCurl
19
     */
20
    private $clientMulti;
21

22
    /**
23
     * Promise state.
24
     *
25
     * @var string
26
     */
27
    private $state;
28

29
    /**
30
     * Create new promise.
31
     *
32
     * @param MultiCurl $clientMulti
33
     */
34
    public function __construct(MultiCurl $clientMulti)
35
    {
36
        $this->clientMulti = $clientMulti;
12✔
37
        $this->state = Promise::PENDING;
12✔
38
    }
39

40
    /**
41
     * Add behavior for when the promise is resolved or rejected.
42
     *
43
     * If you do not care about one of the cases, you can set the corresponding callable to null
44
     * The callback will be called when the response or exception arrived and never more than once.
45
     *
46
     * @param callable $onComplete Called when a response will be available
47
     * @param callable $onRejected Called when an error happens.
48
     *
49
     * You must always return the Response in the interface or throw an Exception
50
     *
51
     * @return Promise Always returns a new promise which is resolved with value of the executed
52
     *                 callback (onFulfilled / onRejected)
53
     */
54
    public function then(callable $onComplete = null, callable $onRejected = null)
55
    {
56
        if ($onComplete) {
12✔
57
            $this->clientMulti->complete(
12✔
58
                static function (Curl $instance) use ($onComplete) {
12✔
59
                    if ($instance->request instanceof \Httpful\Request) {
12✔
60
                        $response = $instance->request->_buildResponse($instance->rawResponse, $instance);
12✔
61
                    } else {
62
                        $response = $instance->rawResponse;
×
63
                    }
64

65
                    $onComplete(
12✔
66
                        $response,
12✔
67
                        $instance->request,
12✔
68
                        $instance
12✔
69
                    );
12✔
70
                }
12✔
71
            );
12✔
72
        }
73

74
        if ($onRejected) {
12✔
75
            $this->clientMulti->error(
×
76
                static function (Curl $instance) use ($onRejected) {
×
77
                    if ($instance->request instanceof \Httpful\Request) {
×
78
                        $response = $instance->request->_buildResponse($instance->rawResponse, $instance);
×
79
                    } else {
80
                        $response = $instance->rawResponse;
×
81
                    }
82

83
                    $onRejected(
×
84
                        $response,
×
85
                        $instance->request,
×
86
                        $instance
×
87
                    );
×
88
                }
×
89
            );
×
90
        }
91

92
        return new self($this->clientMulti);
12✔
93
    }
94

95
    /**
96
     * Get the state of the promise, one of PENDING, FULFILLED or REJECTED.
97
     *
98
     * @return string
99
     */
100
    public function getState()
101
    {
102
        return $this->state;
×
103
    }
104

105
    /**
106
     * Wait for the promise to be fulfilled or rejected.
107
     *
108
     * When this method returns, the request has been resolved and the appropriate callable has terminated.
109
     *
110
     * When called with the unwrap option
111
     *
112
     * @param bool $unwrap Whether to return resolved value / throw reason or not
113
     *
114
     * @return MultiCurl|null Resolved value, null if $unwrap is set to false
115
     */
116
    public function wait($unwrap = true)
117
    {
118
        if ($unwrap) {
12✔
119
            $this->clientMulti->start();
12✔
120
            $this->state = Promise::FULFILLED;
12✔
121

122
            return $this->clientMulti;
12✔
123
        }
124

125
        try {
126
            $this->clientMulti->start();
×
127
            $this->state = Promise::FULFILLED;
×
128
        } catch (\ErrorException $e) {
×
129
            $this->_error((string) $e);
×
130
        }
131

132
        return null;
×
133
    }
134

135
    /**
136
     * @param string $error
137
     *
138
     * @return void
139
     */
140
    private function _error($error)
141
    {
142
        $this->state = Promise::REJECTED;
×
143

144
        // global error handling
145

146
        $global_error_handler = \Httpful\Setup::getGlobalErrorHandler();
×
147
        if ($global_error_handler) {
×
148
            if ($global_error_handler instanceof \Psr\Log\LoggerInterface) {
×
149
                // PSR-3 https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
150
                $global_error_handler->error($error);
×
151
            } elseif (\is_callable($global_error_handler)) {
×
152
                // error callback
153
                /** @noinspection VariableFunctionsUsageInspection */
154
                \call_user_func($global_error_handler, $error);
×
155
            }
156
        }
157

158
        // local error handling
159

160
        /** @noinspection ForgottenDebugOutputInspection */
161
        \error_log($error);
×
162
    }
163
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc