GuzzleHttpClient   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createEndpoint() 0 11 1
A writeEndpoints() 0 12 2
A get() 0 12 1
1
<?php
2
3
namespace TomPHP\HalClient\HttpClient;
4
5
use GuzzleHttp\Client;
6
use TomPHP\HalClient\HttpClient;
7
use Zend\Diactoros\Response;
8
9
final class GuzzleHttpClient implements HttpClient
10
{
11
    /** @var string */
12
    private $dbPath;
13
14
    public function __construct()
15
    {
16
        $this->dbPath = __DIR__.'/../../testapi/endpoints.db';
17
18
        $this->writeEndpoints([]);
19
    }
20
21
    /**
22
     * @param string $method
23
     * @param string $url
24
     * @param string $contentType
25
     * @param string $body
26
     */
27
    public function createEndpoint($method, $url, $contentType, $body)
28
    {
29
        $endpoints = unserialize(file_get_contents($this->dbPath));
30
31
        $endpoints[$method][$url] = [
32
            'contentType' => $contentType,
33
            'body'        => $body,
34
        ];
35
36
        $this->writeEndpoints($endpoints);
37
    }
38
39
    private function writeEndpoints($endpoints)
40
    {
41
        $fp = fopen($this->dbPath, 'w');
42
43
        if (!$fp) {
44
            throw new \RuntimeException("Fail to open {$this->dbPath} for writing.");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
45
        }
46
47
        fputs($fp, serialize($endpoints));
48
        fflush($fp);
49
        fclose($fp);
50
    }
51
52
    public function get($url)
53
    {
54
        $client = new Client();
55
56
        $response = $client->get($url);
57
58
        return new Response(
59
            'data://text/plain,'.(string) $response->getBody(),
60
            $response->getStatusCode(),
61
            $response->getHeaders()
62
        );
63
    }
64
}
65