Completed
Push — master ( e247bd...a8cccb )
by Felix
07:53
created

Http::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 4
crap 1
1
<?php
2
namespace Aoe\Varnish\System;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2018 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use GuzzleHttp\Client;
29
use GuzzleHttp\Promise\PromiseInterface;
30
use GuzzleHttp\Psr7\Response;
31
use GuzzleHttp\RequestOptions;
32
33
class Http
34
{
35
    /**
36
     * @var Client
37
     */
38
    private $client;
39
40
    /**
41
     * @var PromiseInterface[]
42
     */
43
    private $promises = [];
44
45
    /**
46
     * @param Client $client
47
     */
48 2
    public function __construct(Client $client)
49
    {
50 2
        $this->client = $client;
51 2
    }
52
53
    /**
54
     * @param string $method
55
     * @param string $url
56
     * @param array $headers
57
     * @param integer $timeout
58
     */
59 2
    public function request($method, $url, $headers = [], $timeout = 0)
60
    {
61 2
        $this->promises[] = $this->client->requestAsync($method, $url, [
62 2
            RequestOptions::HEADERS => $headers,
63 2
            RequestOptions::TIMEOUT => $timeout
64 2
        ]);
65 2
    }
66
67
    /**
68
     * @return array
69
     * @throws \Exception|\Throwable
70
     */
71
    public function wait()
72
    {
73
        $phrases = [];
74
        $results = \GuzzleHttp\Promise\settle($this->promises)->wait();
75
        foreach ($results as $result) {
76
            if ($result['state'] === 'fulfilled') {
77
                $response = $result['value'];
78
                if ($response instanceof Response) {
79
                    $phrases[] = [
80
                        'reason' => $response->getReasonPhrase(),
81
                        'success' => $response->getStatusCode() === 200
82
                    ];
83
                }
84
            } else {
85
                if ($result['state'] === 'rejected') {
86
                    $reason = $result['reason'];
87
                    if ($reason instanceof \Exception) {
88
                        $reason = $reason->getMessage();
89
                    }
90
                    $phrases[] = [
91
                        'reason' => $reason,
92
                        'success' => false
93
                    ];
94
                } else {
95
                    $phrases[] = [
96
                        'reason' => 'unknown exception',
97
                        'success' => false
98
                    ];
99
                }
100
            }
101
        }
102
        return $phrases;
103
    }
104
}
105