Metadata::jsonSerialize()   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 0
crap 1
1
<?php
2
3
namespace GoCardlessPayment\MandateCheckout;
4
5
use GoCardlessPayment\Makeable;
6
7
class Metadata implements \JsonSerializable
8
{
9
    use Makeable;
10
11
    public array $data = [];
12
13
    /**
14
     * @throws \Exception
15
     */
16 1
    public function add(string $key, string $value): static
17
    {
18 1
        if (count($this->data) >= 3) {
19
            throw new \Exception('Max 3 items allowed in metadata.');
20
        }
21 1
        if (strlen($key) > 50) {
22
            throw new \Exception('Key length max - 50 chars.');
23
        }
24 1
        if (strlen($value) > 500) {
25
            throw new \Exception('Value length max - 500 chars.');
26
        }
27
28 1
        $this->data[$key] = $value;
29
30 1
        return $this;
31
    }
32
33 1
    public function jsonSerialize(): array
34
    {
35 1
        return array_filter($this->data, fn ($i) => ! is_null($i));
36
    }
37
}
38