BillingRequest::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace GoCardlessPayment\MandateCheckout;
4
5
use GoCardlessPayment\Makeable;
6
7
class BillingRequest implements \JsonSerializable
8
{
9
    use Makeable;
10
11
    protected array $params = [];
12
13
    protected bool $fallbackEnabled = false;
14
15
    protected ?MandateRequest $mandateRequest = null;
16
17
    protected ?Metadata $metadata = null;
18
19
    protected ?Links $links = null;
20
21 1
    public function __construct(array $params = [])
22
    {
23 1
        $this->params = $params;
24
    }
25
26
    /**
27
     * @see https://developer.gocardless.com/api-reference#billing-requests-create-a-billing-request
28
     */
29
    public function enableFallback(bool $fallbackEnabled = true): static
30
    {
31
        $this->fallbackEnabled = $fallbackEnabled;
32
33
        return $this;
34
    }
35
36 1
    public function mandateRequest(?MandateRequest $mandateRequest = null): static
37
    {
38 1
        $this->mandateRequest = $mandateRequest;
39
40 1
        return $this;
41
    }
42
43
    public function metadata(?Metadata $metadata): static
44
    {
45
        $this->metadata = $metadata;
46
47
        return $this;
48
    }
49
50 1
    public function links(?Links $links): static
51
    {
52 1
        $this->links = $links;
53
54 1
        return $this;
55
    }
56
57 1
    public function jsonSerialize(): array
58
    {
59 1
        $params = [];
60
61 1
        if ($this->fallbackEnabled) {
62
            $params['fallback_enabled'] = $this->fallbackEnabled;
63
        }
64
65 1
        if ($this->mandateRequest) {
66 1
            $params['mandate_request'] = $this->mandateRequest->jsonSerialize();
67
        }
68
69 1
        if ($this->metadata) {
70
            $params['metadata'] = $this->metadata->jsonSerialize();
71
        }
72
73 1
        if ($this->links) {
74 1
            $params['links'] = $this->links->jsonSerialize();
75
        }
76
77 1
        return array_merge($this->params, array_filter($params, fn ($i) => ! is_null($i) && (is_array($i) && ! empty($i))));
78
    }
79
}
80