Completed
Push — master ( 21f6d2...0d5a3d )
by Kevin
07:16
created

Http::wait()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
ccs 0
cts 23
cp 0
rs 8.439
cc 5
eloc 20
nc 5
nop 0
crap 30
1
<?php
2
namespace Aoe\Varnish\System;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\Promise\PromiseInterface;
6
use GuzzleHttp\Psr7\Response;
7
8
class Http
9
{
10
    /**
11
     * @var Client
12
     */
13
    private $client;
14
15
    /**
16
     * @var PromiseInterface[]
17
     */
18
    private $promises = [];
19
20
    /**
21
     * @param Client $client
22
     */
23
    public function __construct(Client $client)
24
    {
25
        $this->client = $client;
26
    }
27
28
    /**
29
     * @param string $method
30
     * @param string $url
31
     * @param array $headers
32
     */
33
    public function request($method, $url, $headers = [])
34
    {
35
        $this->promises[] = $this->client->requestAsync($method, $url, [
36
            'headers' => $headers
37
        ]);
38
    }
39
40
    /**
41
     * @return array
42
     * @throws \Exception|\Throwable
43
     */
44
    public function wait()
45
    {
46
        $phrases = [];
47
        $results = \GuzzleHttp\Promise\settle($this->promises)->wait();
48
        foreach ($results as $result) {
49
            if ($result['state'] === 'fulfilled') {
50
                $response = $result['value'];
51
                if ($response instanceof Response) {
52
                    $phrases[] = [
53
                        'reason' => $response->getReasonPhrase(),
54
                        'success' => $response->getStatusCode() === 200
55
                    ];
56
                }
57
            } else {
58
                if ($result['state'] === 'rejected') {
59
                    $phrases[] = [
60
                        'reason' => $result['reason'],
61
                        'success' => false
62
                    ];
63
                } else {
64
                    $phrases[] = [
65
                        'reason' => 'unknown exception',
66
                        'success' => false
67
                    ];
68
                }
69
            }
70
        }
71
        return $phrases;
72
    }
73
}
74