Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractBasket
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
6 / 6
11
100.00% covered (success)
100.00%
1 / 1
 applyOffer
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getAppliedOffer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 add
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTotal
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getTotalFormatted
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Alistaircol\Hta\Domain\Basket;
6
7use Alistaircol\Hta\Domain\Basket\Concerns\BasketInterface;
8use Alistaircol\Hta\Domain\Basket\Concerns\OfferInterface;
9use Alistaircol\Hta\Domain\Basket\Concerns\ProductInterface;
10use Alistaircol\Hta\Domain\Basket\DataTransferObjects\ProductInterfaceCollection;
11use Alistaircol\Hta\Domain\Basket\Exceptions\OfferDiscountOutOfBoundsException;
12use Alistaircol\Hta\Domain\Basket\Exceptions\ProductPriceOutOfBoundsException;
13
14abstract class AbstractBasket implements BasketInterface
15{
16    protected ProductInterfaceCollection $items;
17    protected ?OfferInterface $offer = null;
18
19    /**
20     * @inheritDoc
21     */
22    public function applyOffer(OfferInterface $offer): BasketInterface
23    {
24        $multiplier = $offer->getDiscountMultiplier();
25        if ($multiplier < 0 || $multiplier > 100) {
26            throw OfferDiscountOutOfBoundsException::causer($offer);
27        }
28
29        $this->offer = $offer;
30
31        return $this;
32    }
33
34    /**
35     * @inheritDoc
36     */
37    public function getAppliedOffer(): ?OfferInterface
38    {
39        return $this->offer;
40    }
41
42    public function add(ProductInterface $product): BasketInterface
43    {
44        if ($product->getPrice() < 0) {
45            throw ProductPriceOutOfBoundsException::causer($product);
46        }
47
48        return $this;
49    }
50
51    public function get(string $id): ?ProductInterface
52    {
53        return $this->items->offsetGet($id);
54    }
55
56    /**
57     * @inheritDoc
58     */
59    public function getTotal(): int
60    {
61        if ($this->items->count() === 0) {
62            return 0;
63        }
64
65        $total = 0;
66        foreach ($this->items as $product) {
67            $total += $product->getDiscountedPrice($this->offer);
68        }
69
70        return $total;
71    }
72
73    /**
74     * @inheritDoc
75     */
76    public function getTotalFormatted(int $total, ?string $iso4217 = 'GBP'): string
77    {
78        $formatter = new PriceFormatter($iso4217);
79        return $formatter->format($total);
80    }
81}