WrapsGuzzle::requestAsync()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Muzzle;
4
5
use GuzzleHttp\Promise\PromiseInterface;
6
use Muzzle\Messages\AssertableResponse;
7
use OutOfBoundsException;
8
use PHPUnit\Framework\Assert as PHPUnit;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\UriInterface;
12
use Symfony\Component\VarDumper\Cloner\VarCloner;
13
use Symfony\Component\VarDumper\Dumper\AbstractDumper;
14
use Symfony\Component\VarDumper\Dumper\CliDumper;
15
16
/**
17
 * @method AssertableResponse get(string | UriInterface $uri, array $options = [])
18
 * @method AssertableResponse head(string | UriInterface $uri, array $options = [])
19
 * @method AssertableResponse put(string | UriInterface $uri, array $options = [])
20
 * @method AssertableResponse post(string | UriInterface $uri, array $options = [])
21
 * @method AssertableResponse patch(string | UriInterface $uri, array $options = [])
22
 * @method AssertableResponse delete(string | UriInterface $uri, array $options = [])
23
 * @method PromiseInterface getAsync(string | UriInterface $uri, array $options = [])
24
 * @method PromiseInterface headAsync(string | UriInterface $uri, array $options = [])
25
 * @method PromiseInterface putAsync(string | UriInterface $uri, array $options = [])
26
 * @method PromiseInterface postAsync(string | UriInterface $uri, array $options = [])
27
 * @method PromiseInterface patchAsync(string | UriInterface $uri, array $options = [])
28
 * @method PromiseInterface deleteAsync(string | UriInterface $uri, array $options = [])
29
 */
30
trait WrapsGuzzle
31
{
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function send(RequestInterface $request, array $options = []) : ResponseInterface
37
    {
38
39
        return $this->delegate('send', $request, $options);
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function sendAsync(RequestInterface $request, array $options = []) : PromiseInterface
46
    {
47
48
        return $this->delegate('sendAsync', $request, $options);
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function request($method, $uri, array $options = []) : ResponseInterface
55
    {
56
57
        return $this->delegate('request', $method, $uri, $options);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function requestAsync($method, $uri, array $options = []) : PromiseInterface
64
    {
65
66
        return $this->delegate('requestAsync', $method, $uri, $options);
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function getConfig($option = null)
73
    {
74
75
        return $this->delegate('getConfig', $option);
76
    }
77
78
    public function __call($method, $arguments)
79
    {
80
81
        return $this->delegate($method, ...$arguments);
82
    }
83
84
    private function delegate(string $method, ...$arguments)
85
    {
86
87
        try {
88
            return $this->client->{$method}(...$arguments);
89
        } catch (OutOfBoundsException $exception) {
90
            $dumper = new CliDumper(null, null, AbstractDumper::DUMP_LIGHT_ARRAY);
91
            PHPUnit::fail(sprintf(
92
                'Mock queue was empty when calling [%s] with the arguments: %s',
93
                $method,
94
                $dumper->dump((new VarCloner)->cloneVar($arguments), true)
95
            ));
96
        }
97
    }
98
}
99