Passed
Push — 6.4 ( 969936...be76f2 )
by Christian
44:06 queued 29:02
created

OrderLineItemCollection   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 21

12 Methods

Rating   Name   Duplication   Size   Complexity  
A sortByCreationDate() 0 8 2
A filterByOrderId() 0 4 1
A getOrderIds() 0 4 1
A sortByPosition() 0 4 1
A getApiAlias() 0 3 1
A getPrices() 0 6 1
A getExpectedClass() 0 3 1
A filterGoodsFlat() 0 12 3
A hasLineItemWithState() 0 9 3
A getPayloadsProperty() 0 9 2
A filterByType() 0 4 1
A buildFlat() 0 16 4
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Order\Aggregate\OrderLineItem;
4
5
use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection;
6
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
7
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
8
use Shopware\Core\Framework\Log\Package;
9
10
/**
11
 * @extends EntityCollection<OrderLineItemEntity>
12
 */
13
#[Package('customer-order')]
14
class OrderLineItemCollection extends EntityCollection
15
{
16
    /**
17
     * @return list<string>
0 ignored issues
show
Bug introduced by
The type Shopware\Core\Checkout\O...gate\OrderLineItem\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
     */
19
    public function getOrderIds(): array
20
    {
21
        return $this->fmap(function (OrderLineItemEntity $orderLineItem) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fmap(function(...) { /* ... */ }) returns the type array which is incompatible with the documented return type Shopware\Core\Checkout\O...gate\OrderLineItem\list.
Loading history...
22
            return $orderLineItem->getOrderId();
23
        });
24
    }
25
26
    public function filterByOrderId(string $id): self
27
    {
28
        return $this->filter(function (OrderLineItemEntity $orderLineItem) use ($id) {
29
            return $orderLineItem->getOrderId() === $id;
30
        });
31
    }
32
33
    public function sortByCreationDate(string $sortDirection = FieldSorting::ASCENDING): void
34
    {
35
        $this->sort(function (OrderLineItemEntity $a, OrderLineItemEntity $b) use ($sortDirection) {
36
            if ($sortDirection === FieldSorting::ASCENDING) {
37
                return $a->getCreatedAt() <=> $b->getCreatedAt();
38
            }
39
40
            return $b->getCreatedAt() <=> $a->getCreatedAt();
41
        });
42
    }
43
44
    public function sortByPosition(): void
45
    {
46
        $this->sort(function (OrderLineItemEntity $a, OrderLineItemEntity $b) {
47
            return $a->getPosition() <=> $b->getPosition();
48
        });
49
    }
50
51
    /**
52
     * @return array<mixed>
53
     */
54
    public function getPayloadsProperty(string $property): array
55
    {
56
        return $this->fmap(function (OrderLineItemEntity $lineItem) use ($property) {
57
            $payload = $lineItem->getPayload() ?? [];
58
            if (\array_key_exists($property, $payload)) {
59
                return $payload[$property];
60
            }
61
62
            return null;
63
        });
64
    }
65
66
    public function filterByType(string $type): self
67
    {
68
        return $this->filter(function (OrderLineItemEntity $lineItem) use ($type) {
69
            return $lineItem->getType() === $type;
70
        });
71
    }
72
73
    /**
74
     * @return OrderLineItemEntity[]
75
     */
76
    public function filterGoodsFlat(): array
77
    {
78
        $lineItems = $this->buildFlat($this);
79
80
        $filtered = [];
81
        foreach ($lineItems as $lineItem) {
82
            if ($lineItem->getGood()) {
83
                $filtered[] = $lineItem;
84
            }
85
        }
86
87
        return $filtered;
88
    }
89
90
    public function hasLineItemWithState(string $state): bool
91
    {
92
        foreach ($this->buildFlat($this) as $lineItem) {
93
            if (\in_array($state, $lineItem->getStates(), true)) {
94
                return true;
95
            }
96
        }
97
98
        return false;
99
    }
100
101
    public function getApiAlias(): string
102
    {
103
        return 'order_line_item_collection';
104
    }
105
106
    public function getPrices(): PriceCollection
107
    {
108
        return new PriceCollection(
109
            array_filter(array_map(static function (OrderLineItemEntity $orderLineItem) {
110
                return $orderLineItem->getPrice();
111
            }, array_values($this->getElements())))
112
        );
113
    }
114
115
    protected function getExpectedClass(): string
116
    {
117
        return OrderLineItemEntity::class;
118
    }
119
120
    /**
121
     * @return OrderLineItemEntity[]
122
     */
123
    private function buildFlat(?OrderLineItemCollection $lineItems): array
124
    {
125
        $flat = [];
126
        if (!$lineItems) {
127
            return $flat;
128
        }
129
130
        foreach ($lineItems as $lineItem) {
131
            $flat[] = $lineItem;
132
133
            foreach ($this->buildFlat($lineItem->getChildren()) as $nest) {
134
                $flat[] = $nest;
135
            }
136
        }
137
138
        return $flat;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $flat returns an array which contains values of type array which are incompatible with the documented value type Shopware\Core\Checkout\O...tem\OrderLineItemEntity.
Loading history...
139
    }
140
}
141