Completed
Pull Request — develop (#19)
by
unknown
05:29 queued 02:09
created

HttpClient   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 52
ccs 0
cts 15
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A requestAsync() 0 7 1
B checkFileUploadRequest() 0 25 3
1
<?php
2
/**
3
 * Extending GuzzleHttp client
4
 */
5
namespace Graviton\ImportExport\Service;
6
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Psr7;
9
use GuzzleHttp\Promise;
10
11
/**
12
 * Class HttpClient
13
 * Extends Guzzle client
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/import-export/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class HttpClient extends Client
20
{
21
    /** @var string */
22
    private $url;
23
24
    /**
25
     * Parse Body and if File is found it will find file and send it
26
     *
27
     * @param string $method  Request method to be used
28
     * @param string $uri     Url to where to send data
29
     * @param array  $options Config params
30
     *
31
     * @return \GuzzleHttp\Promise\PromiseInterface
32
     */
33
    public function requestAsync($method, $uri = null, array $options = [])
34
    {
35
        $this->url = $uri;
36
        $options = $this->checkFileUploadRequest($options);
37
38
        return parent::requestAsync($method, $this->url, $options);
39
    }
40
41
    /**
42
     * @param array $options Curl data options
43
     * @return array options
44
     */
45
    private function checkFileUploadRequest($options)
46
    {
47
        if (!array_key_exists('upload', $options)) {
48
            return $options;
49
        }
50
51
        $fileName = $options['upload'];
52
        unset($options['upload']);
53
54
        if (!$fileName) {
55
            return $options;
56
        }
57
58
        // We send the data in URL
59
        $options['query'] = ['metadata' =>  json_encode($options['json'])];
60
61
        // Guzzle modify header if we send json.
62
        unset($options['json']);
63
64
        // We send file only
65
        $options['body'] = fopen($fileName, 'r');
66
67
        return $options;
68
69
    }
70
}
71