Completed
Pull Request — develop (#18)
by
unknown
09:08 queued 06:09
created

HttpClient::requestAsync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
crap 2
1
<?php
2
/**
3
 * Extending GuzzleHttp client
4
 */
5
namespace Graviton\ImportExport\Service;
6
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Psr7;
9
10
/**
11
 * Class HttpClient
12
 * Extends Guzzle client
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/import-export/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class HttpClient extends Client
19
{
20
    /**
21
     * Parse Body and if File is found it will find file and send it
22
     *
23
     * @param string $method  Request method to be used
24
     * @param string $uri     Url to where to send data
25
     * @param array  $options Config params
26
     *
27
     * @return \GuzzleHttp\Promise\PromiseInterface
28
     */
29
    public function requestAsync($method, $uri = null, array $options = [])
30
    {
31
        $options = $this->checkFileUploadRequest($options);
32
33
        return parent::requestAsync($method, $uri, $options);
34
    }
35
36
    /**
37
     * Parse Body and if File is found it will find file and send it
38
     *
39
     * @param string $method  Request method to be used
40
     * @param string $uri     Url to where to send data
41
     * @param array  $options Config params
42
     * @return mixed
43
     */
44
    public function request($method, $uri = null, array $options = [])
45
    {
46
        $options = $this->checkFileUploadRequest($options);
47
        $options[RequestOptions::SYNCHRONOUS] = true;
48
        return $this->requestAsync($method, $uri, $options)->wait();
49
    }
50
51
    /**
52
     * @param array $options Curl data options
53
     * @return array options
54
     */
55
    private function checkFileUploadRequest($options)
56
    {
57
        $originFileName = array_key_exists('origin', $options) ? $options['origin'] : false;
58
59
        if (!$originFileName) {
60
            return $options;
61
        }
62
        // Remove un-used param
63
        unset($options['origin']);
64
65
        // Is there a file and a @
66
        if (!isset($options['json'])
67
            || !isset($options['json']['file'])
68
            || !strpos($options['json']['file'], '@') == 0) {
69
            return $options;
70
        }
71
72
        // Find file
73
        $fileName = preg_replace('/([^\/]+$)/', substr($options['json']['file'], 1), $originFileName);
74
        if (!file_exists($fileName)) {
75
            return $options;
76
        }
77
        
78
        // We send file only
79
        $options = [];
80
        $options['body'] = fopen(str_replace('//', '/', $fileName), 'r');
81
82
        return $options;
83
84
    }
85
}
86