HttpPlugHttpAdapterClient::buildRequestError()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.2
cc 4
eloc 5
nc 8
nop 1
crap 4
1
<?php
2
3
namespace Rs\VersionEye\Http;
4
5
use Http\Client\Exception\HttpException as PlugException;
6
use Http\Client\HttpClient as PlugClient;
7
use Http\Message\MessageFactory;
8
use Http\Message\MultipartStream\MultipartStreamBuilder;
9
use Psr\Http\Message\RequestInterface;
10
11
/**
12
 * HttpPlugHttpAdapterClient.
13
 *
14
 * @author Robert Schönthal <[email protected]>
15
 */
16
class HttpPlugHttpAdapterClient implements HttpClient
17
{
18
    /**
19
     * @var PlugClient
20
     */
21
    private $adapter;
22
23
    private $url;
24
    /**
25
     * @var MessageFactory
26
     */
27
    private $factory;
28
    /**
29
     * @var MultipartStreamBuilder
30
     */
31
    private $multipartStreamBuilder;
32
33
    /**
34
     * @param PlugClient             $adapter
35
     * @param string                 $url
36
     * @param MessageFactory         $factory
37
     * @param MultipartStreamBuilder $multipartStreamBuilder
38
     */
39 13
    public function __construct(PlugClient $adapter, $url, MessageFactory $factory, MultipartStreamBuilder $multipartStreamBuilder)
40
    {
41 13
        $this->adapter                = $adapter;
42 13
        $this->url                    = $url;
43 13
        $this->factory                = $factory;
44 13
        $this->multipartStreamBuilder = $multipartStreamBuilder;
45 13
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 5
    public function request($method, $path, array $params = [])
51
    {
52 5
        list($params, $files) = $this->splitParams($params);
53 5
        $url                  = $this->url . $path;
54
55
        try {
56 5
            $request  = $this->createRequest($params, $files, $method, $url);
57 5
            $response = $this->adapter->sendRequest($request);
58
59 4
            return json_decode($response->getBody(), true);
60 1
        } catch (PlugException $e) {
61 1
            throw $this->buildRequestError($e);
62
        }
63
    }
64
65
    /**
66
     * splits arguments into parameters and files (if any).
67
     *
68
     * @param array $params
69
     *
70
     * @return array
71
     */
72 5
    private function splitParams(array $params)
73
    {
74 5
        $parameters = [];
75 5
        $files      = [];
76
77 5
        foreach ($params as $name => $value) {
78 2
            if (is_readable($value)) { //file
79 1
                $files[$name] = $value;
80
            } else {
81 2
                $parameters[$name] = $value;
82
            }
83
        }
84
85 5
        return [$parameters, $files];
86
    }
87
88
    /**
89
     * builds the error exception.
90
     *
91
     * @param PlugException $e
92
     *
93
     * @return CommunicationException
94
     */
95 1
    private function buildRequestError(PlugException $e)
96
    {
97 1
        $data    = $e->getResponse() ? json_decode($e->getResponse()->getBody(), true) : ['error' => $e->getMessage()];
98 1
        $message = isset($data['error']) ? $data['error'] : 'Server Error';
99 1
        $status  = $e->getResponse() ? $e->getResponse()->getStatusCode() : 500;
100
101 1
        return new CommunicationException(sprintf('%s : %s', $status, $message));
102
    }
103
104
    /**
105
     * @param array  $params
106
     * @param array  $files
107
     * @param string $method
108
     * @param string $url
109
     *
110
     * @return RequestInterface
111
     */
112 5
    private function createRequest(array $params, array $files, $method, $url)
113
    {
114 5
        if (!count($params) && !count($files)) {
115 3
            return $this->factory->createRequest($method, $url);
116
        }
117
118 2
        $builder = clone $this->multipartStreamBuilder;
119
120 2
        foreach ($params as $k => $v) {
121 2
            $builder->addResource($k, $v);
122
        }
123
124 2
        foreach ($files as $k => $file) {
125 1
            $builder->addResource($k, fopen($file, 'r'), ['filename' => $file]);
126
        }
127
128 2
        return $this->factory->createRequest(
129
            $method,
130
            $url,
131 2
            ['Content-Type' => 'multipart/form-data; boundary=' . $builder->getBoundary()],
132 2
            $builder->build()
133
        );
134
    }
135
}
136