Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
InMemoryBasket
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 create
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 add
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 remove
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getItems
100.00% covered (success)
100.00%
1 / 1
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;
11
12class InMemoryBasket extends AbstractBasket
13{
14    /**
15     * Create a new empty basket with optional offer.
16     *
17     * @param OfferInterface|null $offer
18     * @return BasketInterface
19     */
20    public function create(?OfferInterface $offer = null): BasketInterface
21    {
22        $this->items = new ProductInterfaceCollection();
23
24        if ($offer instanceof OfferInterface) {
25            $this->applyOffer($offer);
26        }
27
28        return $this;
29    }
30
31    /**
32     * Add a product to the basket.
33     *
34     * @param ProductInterface $product
35     * @return BasketInterface
36     */
37    public function add(ProductInterface $product): BasketInterface
38    {
39        parent::add($product);
40        $this->items->offsetSet($product->getId(), $product);
41
42        return $this;
43    }
44
45    /**
46     * Remove a product from the basket.
47     *
48     * @param ProductInterface $product
49     * @return BasketInterface
50     */
51    public function remove(ProductInterface $product): BasketInterface
52    {
53        if ($this->items->offsetExists($product->getId())) {
54            $this->items->offsetUnset($product->getId());
55        }
56
57        return $this;
58    }
59
60    /**
61     * Get items in the basket.
62     *
63     * @return ProductInterfaceCollection
64     */
65    public function getItems(): ProductInterfaceCollection
66    {
67        return $this->items;
68    }
69}