Completed
Pull Request — 1.x (#187)
by Akihito
02:07
created

HttpResourceObject   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __get() 0 17 5
A __set() 0 4 1
A __isset() 0 4 1
A __toString() 0 4 1
A request() 0 11 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use function is_array;
8
use function strtoupper;
9
use Symfony\Contracts\HttpClient\HttpClientInterface;
10
use Symfony\Contracts\HttpClient\ResponseInterface;
11
12
/**
13
 * @method HttpResourceObject get(AbstractUri|string $uri, array $params = [])
14
 * @method HttpResourceObject head(AbstractUri|string $uri, array $params = [])
15
 * @method HttpResourceObject put(AbstractUri|string $uri, array $params = [])
16
 * @method HttpResourceObject post(AbstractUri|string $uri, array $params = [])
17
 * @method HttpResourceObject patch(AbstractUri|string $uri, array $params = [])
18
 * @method HttpResourceObject delete(AbstractUri|string $uri, array $params = [])
19
 */
20
final class HttpResourceObject extends ResourceObject
21
{
22
    /**
23
     * @var HttpClientInterface
24
     */
25
    private $client;
26
27
    /**
28
     * @var ResponseInterface
29
     */
30
    private $response;
31
32
    public function __construct(HttpClientInterface $client)
33
    {
34
        $this->client = $client;
35
        unset($this->code, $this->headers, $this->body, $this->view);
36
    }
37
38
    public function __get(string $name)
39
    {
40
        if ($name === 'code') {
41
            return $this->response->getStatusCode();
42
        }
43
        if ($name === 'headers') {
44
            return $this->response->getHeaders();
45
        }
46
        if ($name === 'body') {
47
            return $this->response->toArray();
48
        }
49
        if ($name === 'view') {
50
            return $this->response->getContent();
51
        }
52
53
        throw new \InvalidArgumentException($name);
54
    }
55
56
    public function __set(string $name, $value) : void
57
    {
58
        throw new \InvalidArgumentException($name);
59
    }
60
61
    public function __isset(string $name) : bool
62
    {
63
        return isset($this->{$name});
64
    }
65
66
    public function __toString() : string
67
    {
68
        return $this->response->getContent();
69
    }
70
71
    public function request(AbstractRequest $request)
72
    {
73
        $uri = $request->resourceObject->uri;
74
        $method = strtoupper($uri->method);
75
        $options = ($method === 'GET') ? ['query' => $uri->query] : ['body' => $uri->query];
76
        $clientOptions = isset($uri->query['_options']) && is_array($uri->query['_options']) ? $uri->query['_options'] : [];
77
        $options += $clientOptions;
78
        $this->response = $this->client->request($method, (string) $uri, $options);
79
80
        return $this;
81
    }
82
}
83