Test Failed
Push — master ( b71f19...57db9a )
by Michael
02:20
created

ResourceBasedClient::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Bundle\JsonApiBundle\HttpClient;
5
6
use GuzzleHttp\Psr7\Request;
7
use Mikemirten\Bundle\JsonApiBundle\Routing\RouteRepository;
8
use Mikemirten\Component\JsonApi\Document\AbstractDocument;
9
use Mikemirten\Component\JsonApi\HttpClient\HttpClientInterface;
10
use Mikemirten\Component\JsonApi\HttpClient\JsonApiRequest;
11
use Psr\Http\Message\ResponseInterface;
12
13
/**
14
 * Resources-based HTTP client
15
 * Supports JsonApi requests
16
 *
17
 * @package Mikemirten\Bundle\JsonApiBundle\HttpClient
18
 *
19
 * @method ResponseInterface get(string $resource, array $parameters = [], array $headers = [])
20
 * @method ResponseInterface head(string $resource, array $parameters = [], array $headers = [])
21
 * @method ResponseInterface post(string $resource, array $parameters = [], array $headers = [], $body = null)
22
 * @method ResponseInterface patch(string $resource, array $parameters = [], array $headers = [], $body = null)
23
 * @method ResponseInterface put(string $resource, array $parameters = [], array $headers = [], $body = null)
24
 * @method ResponseInterface delete(string $resource, array $parameters = [], array $headers = [])
25
 * @method ResponseInterface options(string $resource, array $parameters = [], array $headers = [])
26
 *
27
 */
28
class ResourceBasedClient
29
{
30
    /**
31
     * @var HttpClientInterface
32
     */
33
    protected $client;
34
35
    /**
36
     * @var RouteRepository
37
     */
38
    protected $repository;
39
40
    /**
41
     * ResourceBasedClient constructor.
42
     *
43
     * @param HttpClientInterface $client
44
     * @param RouteRepository     $repository
45
     */
46
    public function __construct(HttpClientInterface $client, RouteRepository $repository)
47
    {
48
        $this->client     = $client;
49
        $this->repository = $repository;
50
    }
51
52
    /**
53
     * Send a request
54
     *
55
     * @param  string $method
56
     * @param  string $resource
57
     * @param  array  $parameters
58
     * @param  array  $headers
59
     * @param  mixed  $body
60
     *
61
     * @return ResponseInterface
62
     */
63
    public function request(string $method, string $resource, array $parameters = [], array $headers = [], $body = null): ResponseInterface
64
    {
65
        $uri = $this->repository->generate($resource, $parameters);
66
67
        $request = ($body instanceof AbstractDocument)
68
            ? new JsonApiRequest($method, $uri, $headers, $body)
69
            : new Request($method, $uri, $headers, $body);
70
71
        return $this->client->request($request);
72
    }
73
74
    /**
75
     * Send a request using name of method as an HTTP-method
76
     *
77
     * $client->get('user', ['id' => 1])
78
     * is equal to:
79
     * $client->request('GET', 'user', ['id' => 1])
80
     *
81
     * @param  string $name
82
     * @param  array  $arguments
83
     * @return ResponseInterface
84
     */
85
    public function __call(string $name, array $arguments): ResponseInterface
86
    {
87
        array_unshift(strtoupper($name), $arguments);
0 ignored issues
show
Bug introduced by
strtoupper($name) cannot be passed to array_unshift() as the parameter $array expects a reference.
Loading history...
88
89
        return call_user_func_array([$this, 'request'], $arguments);
90
    }
91
}