Passed
Push — master ( 239936...97d661 )
by Christian
16:56 queued 06:22
created

OrderLineItemCollection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 69
rs 10
c 0
b 0
f 0
ccs 2
cts 4
cp 0.5
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getApiAlias() 0 3 1
A sortByCreationDate() 0 8 2
A filterByOrderId() 0 4 1
A getOrderIds() 0 4 1
A getPrices() 0 6 1
A sortByPosition() 0 4 1
A getExpectedClass() 0 3 1
A getPayloadsProperty() 0 8 2
A filterByType() 0 4 1
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
9
/**
10
 * @method void                     add(OrderLineItemEntity $entity)
11
 * @method void                     set(string $key, OrderLineItemEntity $entity)
12
 * @method OrderLineItemEntity[]    getIterator()
13
 * @method OrderLineItemEntity[]    getElements()
14
 * @method OrderLineItemEntity|null get(string $key)
15
 * @method OrderLineItemEntity|null first()
16
 * @method OrderLineItemEntity|null last()
17
 */
18
class OrderLineItemCollection extends EntityCollection
19
{
20
    public function getOrderIds(): array
21
    {
22
        return $this->fmap(function (OrderLineItemEntity $orderLineItem) {
23 24
            return $orderLineItem->getOrderId();
24
        });
25 24
    }
26
27
    public function filterByOrderId(string $id): self
28
    {
29
        return $this->filter(function (OrderLineItemEntity $orderLineItem) use ($id) {
30
            return $orderLineItem->getOrderId() === $id;
31
        });
32
    }
33
34
    public function sortByCreationDate(string $sortDirection = FieldSorting::ASCENDING): void
35
    {
36
        $this->sort(function (OrderLineItemEntity $a, OrderLineItemEntity $b) use ($sortDirection) {
37
            if ($sortDirection === FieldSorting::ASCENDING) {
38
                return $a->getCreatedAt() > $b->getCreatedAt();
39
            }
40
41
            return $a->getCreatedAt() < $b->getCreatedAt();
42
        });
43
    }
44
45
    public function sortByPosition(): void
46
    {
47
        $this->sort(function (OrderLineItemEntity $a, OrderLineItemEntity $b) {
48
            return $a->getPosition() > $b->getPosition();
49
        });
50
    }
51
52
    public function getPayloadsProperty(string $property): array
53
    {
54
        return $this->fmap(function (OrderLineItemEntity $lineItem) use ($property) {
55
            if (array_key_exists($property, $lineItem->getPayload())) {
0 ignored issues
show
Bug introduced by
It seems like $lineItem->getPayload() can also be of type null; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
            if (array_key_exists($property, /** @scrutinizer ignore-type */ $lineItem->getPayload())) {
Loading history...
56
                return $lineItem->getPayload()[$property];
57
            }
58
59
            return null;
60
        });
61
    }
62
63
    public function filterByType(string $type): self
64
    {
65
        return $this->filter(function (OrderLineItemEntity $lineItem) use ($type) {
66
            return $lineItem->getType() === $type;
67
        });
68
    }
69
70
    public function getApiAlias(): string
71
    {
72
        return 'order_line_item_collection';
73
    }
74
75
    public function getPrices(): PriceCollection
76
    {
77
        return new PriceCollection(
78
            array_filter(array_map(static function (OrderLineItemEntity $orderLineItem) {
79
                return $orderLineItem->getPrice();
80
            }, array_values($this->getElements())))
81
        );
82
    }
83
84
    protected function getExpectedClass(): string
85
    {
86
        return OrderLineItemEntity::class;
87
    }
88
}
89