GuzzleClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Meare\Juggler\HttpClient;
4
5
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\ClientException;
8
use Meare\Juggler\Exception\Mountebank\MountebankException;
9
use Meare\Juggler\Exception\MountebankExceptionFactory;
10
11
class GuzzleClient implements IHttpClient
12
{
13
    /**
14
     * @var Client
15
     */
16
    private $guzzleClient;
17
18
    /**
19
     * @var MountebankExceptionFactory
20
     */
21
    private $exceptionFactory;
22
23
    /**
24
     * @var string
25
     */
26
    private $host;
27
28
    /**
29
     * @param Client                     $client
30
     * @param MountebankExceptionFactory $exceptionFactory
31
     */
32 15
    public function __construct(Client $client, MountebankExceptionFactory $exceptionFactory)
33
    {
34 15
        $this->guzzleClient = $client;
35 15
        $this->exceptionFactory = $exceptionFactory;
36 15
    }
37
38
    /**
39
     * @return GuzzleClient
40
     */
41 7
    public static function create()
42
    {
43 7
        return new self(
44 7
            new Client, new MountebankExceptionFactory
45 7
        );
46
    }
47
48
    /**
49
     * @param string $host
50
     */
51 14
    public function setHost($host)
52
    {
53 14
        $this->host = rtrim($host, '/');
54 14
    }
55
56
    /**
57
     * @param string $path
58
     * @return string
59
     * @throws MountebankException
60
     */
61 2
    public function get($path)
62
    {
63 2
        return (string)$this->request('GET', $path);
64
    }
65
66
    /**
67
     * @param string      $method
68
     * @param string      $path
69
     * @param string|null $data
70
     * @return string
71
     * @throws MountebankException
72
     */
73 8
    public function request($method, $path, $data = null)
74
    {
75 8
        if (!$this->hasHost()) {
76 1
            throw new \LogicException('Host not set; Unable to perform request');
77
        }
78 7
        $options = [];
79 7
        if (null !== $data) {
80 3
            $options['body'] = $data;
81 3
        }
82
        try {
83 7
            return $this->guzzleClient
84 7
                ->request($method, $this->getUrl($path), $options)
85 6
                ->getBody();
86 1
        } catch (ClientException $e) {
87 1
            throw $this->convertToMountebankException($e);
88
        }
89
    }
90
91
    /**
92
     * @return bool
93
     */
94 8
    public function hasHost()
95
    {
96 8
        return null !== $this->host;
97
    }
98
99
    /**
100
     * @param string $path
101
     * @return string
102
     */
103 7
    private function getUrl($path)
104
    {
105 7
        return $this->host . $path;
106
    }
107
108
    /**
109
     * Creates MountebankException instance from mountebank response
110
     *
111
     * @param ClientException $e
112
     * @return MountebankException
113
     */
114 1
    private function convertToMountebankException(ClientException $e)
115
    {
116 1
        return $this->exceptionFactory->createInstanceFromMountebankResponse(
117 1
            (string)$e->getResponse()->getBody()
118 1
        );
119
    }
120
121
    /**
122
     * @param string $path
123
     * @param string $data
124
     * @return string
125
     * @throws MountebankException
126
     */
127 1
    public function post($path, $data)
128 1
    {
129 1
        return (string)$this->request('POST', $path, $data);
130
    }
131
132
    /**
133
     * @param string $path
134
     * @param string $data
135
     * @return string
136
     * @throws MountebankException
137
     */
138 1
    public function put($path, $data)
139
    {
140 1
        return (string)$this->request('PUT', $path, $data);
141
    }
142
143
    /**
144
     * @param string $path
145
     * @return string
146
     * @throws MountebankException
147
     */
148 1
    public function delete($path)
149
    {
150 1
        return (string)$this->request('DELETE', $path);
151
    }
152
}