Lines 90.90% 20 / 22
Methods 50.00% 2 / 4
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 9 / 9 100.00% 1 / 1 1
 ensureValid 50.00% 1 / 2 0.00% 0 / 1 2.50
 toArray 100.00% 10 / 10 100.00% 1 / 1 1
 jsonSerialize 0.00% 0 / 1 0.00% 0 / 1 2
12class Service extends Entity implements JsonSerializable
13{
14    public static $schema = 'citizenapi/service.json';
15    public int $id;
16    public string $name;
17    public ?int $maxQuantity = null;
18    public ?Combinable $combinable = null;
19    public ?int $parentId = null;
20    public ?int $variantId = null;
21    public ?int $rootParentId = null;
22    public ?bool $showOnStartPage = null;
23
24    public function __construct(
25        int $id,
26        string $name,
27        ?int $maxQuantity = null,
28        ?Combinable $combinable = null,
29        ?int $parentId = null,
30        ?int $variantId = null,
31        ?bool $showOnStartPage = null,
32        ?int $rootParentId = null
33    ) {
34        $this->id = $id;
35        $this->name = $name;
36        $this->maxQuantity = $maxQuantity;
37        $this->combinable = $combinable;
38        $this->parentId = $parentId;
39        $this->variantId = $variantId;
40        $this->showOnStartPage = $showOnStartPage;
41        $this->rootParentId = $rootParentId ?? $id;
42        $this->ensureValid();
43    }
44
45    /**
46     * @return void
47     */
48    private function ensureValid()
49    {
50        if (!$this->testValid()) {
51            throw new InvalidArgumentException("The provided data is invalid according to the schema.");
52        }
53    }
54
55    public function toArray(): array
56    {
57        return [
58            'id' => $this->id,
59            'name' => $this->name,
60            'maxQuantity' => $this->maxQuantity,
61            'combinable' => $this->combinable,
62            'parentId' => $this->parentId,
63            'variantId' => $this->variantId,
64            'rootParentId' => $this->rootParentId,
65            'showOnStartPage' => $this->showOnStartPage
66        ];
67    }
68
69    #[\Override]
70    public function jsonSerialize(): mixed
71    {
72        return $this->toArray();
73    }
74}