Passed
Push — trunk ( 17fe52...3af76c )
by Christian
12:45 queued 12s
created

DiscountPackage::hasMap()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 0
dl 0
loc 17
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Promotion\Cart\Discount;
4
5
use Shopware\Core\Checkout\Cart\CartException;
6
use Shopware\Core\Checkout\Cart\LineItem\Group\LineItemQuantityCollection;
7
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
8
use Shopware\Core\Checkout\Cart\LineItem\LineItemFlatCollection;
9
use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection;
10
use Shopware\Core\Checkout\Promotion\Exception\PriceNotFoundException;
11
use Shopware\Core\Framework\Log\Package;
12
13
#[Package('checkout')]
14
class DiscountPackage
15
{
16
    private LineItemFlatCollection $cartItems;
17
18
    /**
19
     * @var array<string, LineItem>|null
20
     */
21
    private ?array $hashMap;
22
23
    public function __construct(private LineItemQuantityCollection $metaItems)
24
    {
25
        $this->cartItems = new LineItemFlatCollection();
26
        $this->hashMap = null;
27
    }
28
29
    public function getMetaData(): LineItemQuantityCollection
30
    {
31
        return $this->metaItems;
32
    }
33
34
    public function setMetaItems(LineItemQuantityCollection $metaItems): void
35
    {
36
        $this->metaItems = $metaItems;
37
    }
38
39
    public function getCartItems(): LineItemFlatCollection
40
    {
41
        return $this->cartItems;
42
    }
43
44
    public function getCartItem(string $id): LineItem
45
    {
46
        $map = $this->hasMap();
47
48
        if (isset($map[$id])) {
49
            return $map[$id];
50
        }
51
52
        throw CartException::lineItemNotFound($id);
53
    }
54
55
    public function setCartItems(LineItemFlatCollection $items): void
56
    {
57
        $this->cartItems = $items;
58
    }
59
60
    /**
61
     * Gets the overall total price of all cart items in this package.
62
     */
63
    public function getTotalPrice(): float
64
    {
65
        $price = 0;
66
67
        foreach ($this->cartItems as $item) {
68
            if ($item->getPrice() !== null) {
69
                $price += $item->getPrice()->getTotalPrice();
70
            }
71
        }
72
73
        return $price;
74
    }
75
76
    /**
77
     * Gets the price collection of all cart items in this package.
78
     *
79
     * @throws PriceNotFoundException
80
     */
81
    public function getAffectedPrices(): PriceCollection
82
    {
83
        $affectedPrices = new PriceCollection();
84
85
        foreach ($this->cartItems as $lineItem) {
86
            if ($lineItem->getPrice() === null) {
87
                throw new PriceNotFoundException($lineItem);
0 ignored issues
show
Bug introduced by
$lineItem of type array is incompatible with the type Shopware\Core\Checkout\Cart\LineItem\LineItem expected by parameter $item of Shopware\Core\Checkout\P...xception::__construct(). ( Ignorable by Annotation )

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

87
                throw new PriceNotFoundException(/** @scrutinizer ignore-type */ $lineItem);
Loading history...
88
            }
89
90
            $affectedPrices->add($lineItem->getPrice());
91
        }
92
93
        return $affectedPrices;
94
    }
95
96
    /**
97
     * @return array<string, LineItem>
98
     */
99
    private function hasMap(): array
100
    {
101
        if ($this->hashMap !== null) {
102
            return $this->hashMap;
103
        }
104
105
        $this->hashMap = [];
106
        foreach ($this->cartItems as $item) {
107
            // previous implementation always took the first element which maps the id
108
            // to prevent side effects, we keep this logic
109
            if (isset($this->hashMap[$item->getId()])) {
110
                continue;
111
            }
112
            $this->hashMap[$item->getId()] = $item;
113
        }
114
115
        return $this->hashMap;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hashMap returns the type null which is incompatible with the type-hinted return array.
Loading history...
116
    }
117
}
118