Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
27.59% covered (danger)
27.59%
8 / 29
44.44% covered (danger)
44.44%
4 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Order
27.59% covered (danger)
27.59%
8 / 29
44.44% covered (danger)
44.44%
4 / 9
88.43
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOrderStatus
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 provideIndex
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOrderLines
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addOrderLine
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 removeOrderLine
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 provideAllowedMethods
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 acceptOrder
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Apie\Fixtures\Entities;
3
4use APie\Core\Attributes\Internal;
5use Apie\Core\Attributes\ProvideIndex;
6use Apie\Core\Attributes\SearchFilterOption;
7use Apie\Core\Entities\EntityWithStatesInterface;
8use Apie\Core\Entities\RootAggregate;
9use Apie\Core\Lists\StringList;
10use Apie\Fixtures\Enums\OrderStatus;
11use Apie\Fixtures\Identifiers\OrderIdentifier;
12use Apie\Fixtures\Identifiers\OrderLineIdentifier;
13use Apie\Fixtures\Lists\OrderLineList;
14
15#[ProvideIndex('provideIndex')]
16class Order implements RootAggregate, EntityWithStatesInterface
17{
18    private OrderStatus $orderStatus;
19
20    public function __construct(private OrderIdentifier $id, private OrderLineList $orderLines)
21    {
22        $this->orderStatus = OrderStatus::DRAFT;
23    }
24
25    public function getOrderStatus(): OrderStatus
26    {
27        return $this->orderStatus;
28    }
29
30    #[Internal()]
31    public function provideIndex(): array
32    {
33        $result = [];
34        $result[$this->id->toNative()] = 2;
35        foreach ($this->orderLines as $orderLine) {
36            $result[$orderLine->getId()->toNative()] = 1;
37        }
38        return $result;
39    }
40
41    public function getId(): OrderIdentifier
42    {
43        return $this->id;
44    }
45
46    #[SearchFilterOption(enabled: false)]
47    public function getOrderLines(): OrderLineList
48    {
49        return $this->orderLines;
50    }
51
52    public function addOrderLine(OrderLine... $orderLines): int
53    {
54        $this->orderStatus->ensureDraft();
55        foreach ($orderLines as $orderLine) {
56            $this->orderLines[] = clone $orderLine;
57        }
58        return count($this->orderLines);
59    }
60
61    public function removeOrderLine(OrderLineIdentifier $id): ?int
62    {
63        $this->orderStatus->ensureDraft();
64        $id = $id->toNative();
65        foreach ($this->orderLines as $key => $orderLine) {
66            if ($orderLine->getId()->toNative() === $id) {
67                unset($this->orderLines[$key]);
68                return $key;
69            }
70        }
71        return null;
72    }
73
74    public function provideAllowedMethods(): StringList
75    {
76        if ($this->orderStatus === OrderStatus::DRAFT) {
77            return new StringList([
78                'addOrderLine',
79                'removeOrderLine',
80                'acceptOrder'
81            ]);
82        }
83
84        return new StringList([]);
85    }
86
87    public function acceptOrder(): void
88    {
89        $this->orderStatus->ensureDraft();
90        $this->orderStatus = OrderStatus::ACCEPTED;
91    }
92}