Metadata::add()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.5923

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 15
ccs 6
cts 9
cp 0.6667
rs 10
cc 4
nc 4
nop 2
crap 4.5923
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