Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
29.03% covered (danger)
29.03%
9 / 31
55.56% covered (warning)
55.56%
5 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Order
29.03% covered (danger)
29.03%
9 / 31
55.56% covered (warning)
55.56%
5 / 9
84.05
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
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 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 / 4
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\Not;
6use Apie\Core\Attributes\ProvideIndex;
7use Apie\Core\Attributes\RemovalCheck;
8use Apie\Core\Attributes\Requires;
9use Apie\Core\Attributes\SearchFilterOption;
10use Apie\Core\Attributes\StaticCheck;
11use Apie\Core\Attributes\StoreOptions;
12use Apie\Core\ContextConstants;
13use Apie\Core\Entities\EntityWithStatesInterface;
14use Apie\Core\Entities\RootAggregate;
15use Apie\Core\Lists\StringList;
16use Apie\Core\Lists\StringSet;
17use Apie\Fixtures\Enums\OrderStatus;
18use Apie\Fixtures\Identifiers\OrderIdentifier;
19use Apie\Fixtures\Identifiers\OrderLineIdentifier;
20use Apie\Fixtures\Lists\OrderLineList;
21use ReflectionClass;
22
23#[ProvideIndex('provideIndex')]
24#[RemovalCheck(new StaticCheck())]
25class Order implements RootAggregate, EntityWithStatesInterface
26{
27    #[StoreOptions(alwaysMixedData: true)]
28    private OrderStatus $orderStatus;
29
30    #[StaticCheck(new Not(new Requires(ContextConstants::REST_API)))]
31    public ?StringSet $optionalTags = null;
32
33    public function __construct(
34        private OrderIdentifier $id,
35        #[StoreOptions(mutableListField: true)]
36        private OrderLineList $orderLines
37    ) {
38        $this->orderStatus = OrderStatus::DRAFT;
39    }
40
41    public function getOrderStatus(): OrderStatus
42    {
43        return $this->orderStatus;
44    }
45
46    #[Internal()]
47    public function provideIndex(): array
48    {
49        $result = [];
50        $result[$this->id->toNative()] = 2;
51        foreach ($this->orderLines as $orderLine) {
52            $result[$orderLine->getId()->toNative()] = 1;
53        }
54        return $result;
55    }
56
57    public function getId(): OrderIdentifier
58    {
59        return $this->id;
60    }
61
62    #[SearchFilterOption(enabled: false)]
63    public function getOrderLines(): OrderLineList
64    {
65        return $this->orderLines;
66    }
67
68    public function addOrderLine(OrderLine... $orderLines): int
69    {
70        $this->orderStatus->ensureDraft();
71        foreach ($orderLines as $orderLine) {
72            $this->orderLines[] = clone $orderLine;
73        }
74        return count($this->orderLines);
75    }
76
77    public function removeOrderLine(OrderLineIdentifier $id): ?int
78    {
79        $this->orderStatus->ensureDraft();
80        $id = $id->toNative();
81        foreach ($this->orderLines as $key => $orderLine) {
82            if ($orderLine->getId()->toNative() === $id) {
83                unset($this->orderLines[$key]);
84                return $key;
85            }
86        }
87        return null;
88    }
89
90    public function provideAllowedMethods(): StringList
91    {
92        if ($this->orderStatus === OrderStatus::DRAFT) {
93            return new StringList([
94                'addOrderLine',
95                'removeOrderLine',
96                'acceptOrder'
97            ]);
98        }
99
100        return new StringList([]);
101    }
102
103    public function acceptOrder(): void
104    {
105        (new ReflectionClass(OrderLineList::class))->getProperty('mutable')
106            ->setValue($this->orderLines, false);
107        $this->orderStatus->ensureDraft();
108        $this->orderStatus = OrderStatus::ACCEPTED;
109    }
110}