ApiRequest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 91
ccs 37
cts 37
cp 1
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A body() 0 6 1
A addOption() 0 5 1
A overrideOptions() 0 5 1
A __construct() 0 13 1
A getCallBody() 0 5 1
A addNewCall() 0 9 3
A setApiCall() 0 5 1
A call() 0 3 1
1
<?php
2
3
4
namespace LaravelSixConnex;
5
6
use Illuminate\Http\Client\PendingRequest;
7
use Illuminate\Support\Facades\Http;
8
9
class ApiRequest implements CallBody
10
{
11
    protected PendingRequest $client;
12
13
    protected string $username;
14
    protected string $password;
15
    protected string $url;
16
17
    protected ?CallBody $call = null;
18
    /**
19
     * @var CallBody[]
20
     */
21
    protected array $multiplicity = [];
22
23
    /**
24
     * SixConnexRequestBuilder constructor.
25
     *
26
     * @param string $username
27
     * @param string $password
28
     * @param string $url
29
     * @param array $options
30
     */
31 6
    public function __construct(string $username, string $password, string $url, array $options = [])
32
    {
33 6
        $this->username = $username;
34 6
        $this->password = $password;
35 6
        $this->url      = $url;
36
37 6
        $this->call = new SixConnexCall();
38
39 6
        $this->client = Http::withOptions($options['guzzle'] ?? [])
40 6
                            ->withHeaders(array_merge([
41 6
                                'Content-Type' => 'application/json',
42 6
                                'Accept'       => 'application/json',
43 6
                            ], ($options['headers'] ?? [])));
44
    }
45
46 1
    public function addNewCall($call): self
47
    {
48 1
        if (is_array($call)) {
49 1
            $this->multiplicity = array_merge($this->multiplicity, $call);
50 1
        } elseif ($call instanceof CallBody) {
51 1
            $this->multiplicity[] = $call;
52
        }
53
54 1
        return $this;
55
    }
56
57 5
    public function addOption($key, $value = null): self
58
    {
59 5
        $this->call->addOption($key, $value);
0 ignored issues
show
Bug introduced by
The method addOption() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        $this->call->/** @scrutinizer ignore-call */ 
60
                     addOption($key, $value);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
61 5
        return $this;
62
    }
63
64 2
    public function setApiCall(string $type = 'read'): self
65
    {
66 2
        $this->call->setApiCall($type);
67
68 2
        return $this;
69
    }
70
71 1
    public function overrideOptions(array $options): self
72
    {
73 1
        $this->call->overrideOptions($options);
74
75 1
        return $this;
76
    }
77
78 4
    public function getCallBody(): array
79
    {
80 4
        return array_map(fn (CallBody $call) => $call->getCallBody(), array_merge([
81 4
            $this->call,
82 4
        ], $this->multiplicity));
83
    }
84
85 1
    protected function body(): array
86
    {
87 1
        return [
88 1
            'apiUsername'     => $this->username,
89 1
            'apiPassword'     => $this->password,
90 1
            'apicallsetinput' => $this->getCallBody(),
91 1
        ];
92
    }
93
94
    /**
95
     * @return SixConnexResponse
96
     */
97 1
    public function call(): SixConnexResponse
98
    {
99 1
        return new SixConnexResponse($this->client->post($this->url, $this->body()));
100
    }
101
}
102