ShoppingBasket   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 26
c 1
b 0
f 0
dl 0
loc 64
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A whenShoppingBasketWasCheckedOut() 0 4 1
A whenShoppingBasketWasPickedUpByNewCustomer() 0 2 1
A magicallyCreateCustomerId() 0 3 1
A pickUp() 0 10 2
A checkout() 0 5 1
A whenShoppingBasketWasPickedUpByReturningCustomer() 0 3 1
A addItem() 0 4 1
A whenItemWasAddedToShoppingBasket() 0 4 1
A quickCheckout() 0 5 2
1
<?php declare(strict_types=1);
2
namespace Kepawni\Twilted\Basic\TestSample;
3
4
use Assert\Assert;
5
use Kepawni\Twilted\Basic\SimpleAggregateRoot;
6
use Kepawni\Twilted\Basic\TestSample\Event\ItemWasAddedToShoppingBasket;
7
use Kepawni\Twilted\Basic\TestSample\Event\ShoppingBasketWasCheckedOut;
8
use Kepawni\Twilted\Basic\TestSample\Event\ShoppingBasketWasPickedUpByNewCustomer;
9
use Kepawni\Twilted\Basic\TestSample\Event\ShoppingBasketWasPickedUpByReturningCustomer;
10
use Kepawni\Twilted\EntityIdentifier;
11
12
class ShoppingBasket extends SimpleAggregateRoot
13
{
14
    const FAKE_CUSTOMER_ID = '8ccbb53b-6f23-43b8-be73-00efdfb90996';
15
    private $isClosed = false;
16
    private $items = [];
17
    /** @var string|null */
18
    private $returningCustomerId = null;
19
20
    public static function pickUp(EntityIdentifier $uuid, string $returningCustomerId = null)
21
    {
22
        $basket = new static($uuid);
23
        if ($returningCustomerId) {
24
            Assert::that($returningCustomerId)->uuid();
25
            $basket->recordThat(new ShoppingBasketWasPickedUpByReturningCustomer($returningCustomerId));
26
        } else {
27
            $basket->recordThat(new ShoppingBasketWasPickedUpByNewCustomer());
28
        }
29
        return $basket;
30
    }
31
32
    public function addItem(string $itemCode, int $quantity = 1)
33
    {
34
        Assert::that($this->isClosed)->false('Basket has been closed during checkout');
35
        $this->recordThat(new ItemWasAddedToShoppingBasket($itemCode, $quantity));
36
    }
37
38
    public function checkout($billingData, $shippingData, $paymentInformation)
39
    {
40
        Assert::that(array_sum($this->items))->greaterThan(0, 'Empty baskets cannot be processed');
41
        $newCustomerId = $this->magicallyCreateCustomerId($billingData, $shippingData, $paymentInformation);
42
        $this->recordThat(new ShoppingBasketWasCheckedOut($newCustomerId));
43
    }
44
45
    public function quickCheckout()
46
    {
47
        Assert::that(array_sum($this->items))->greaterThan(0, 'Empty baskets cannot be processed');
48
        Assert::that($this->returningCustomerId)->notNull('Quick checkout is possible only for returning customers');
49
        $this->recordThat(new ShoppingBasketWasCheckedOut($this->returningCustomerId ?: ''));
50
    }
51
52
    protected function whenItemWasAddedToShoppingBasket(ItemWasAddedToShoppingBasket $event)
53
    {
54
        $this->items[$event->getItemCode()] = $this->items[$event->getItemCode()] ?? 0;
55
        $this->items[$event->getItemCode()] += $event->getQuantity();
56
    }
57
58
    protected function whenShoppingBasketWasCheckedOut(ShoppingBasketWasCheckedOut $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

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

58
    protected function whenShoppingBasketWasCheckedOut(/** @scrutinizer ignore-unused */ ShoppingBasketWasCheckedOut $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
    {
60
        $this->isClosed = true;
61
        $this->items = [];
62
    }
63
64
    protected function whenShoppingBasketWasPickedUpByNewCustomer(ShoppingBasketWasPickedUpByNewCustomer $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

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

64
    protected function whenShoppingBasketWasPickedUpByNewCustomer(/** @scrutinizer ignore-unused */ ShoppingBasketWasPickedUpByNewCustomer $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
    }
67
68
    protected function whenShoppingBasketWasPickedUpByReturningCustomer(ShoppingBasketWasPickedUpByReturningCustomer $event)
69
    {
70
        $this->returningCustomerId = $event->getReturningCustomerId();
71
    }
72
73
    private function magicallyCreateCustomerId($billingData, $shippingData, $paymentInformation): string
0 ignored issues
show
Unused Code introduced by
The parameter $shippingData is not used and could be removed. ( Ignorable by Annotation )

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

73
    private function magicallyCreateCustomerId($billingData, /** @scrutinizer ignore-unused */ $shippingData, $paymentInformation): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $billingData is not used and could be removed. ( Ignorable by Annotation )

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

73
    private function magicallyCreateCustomerId(/** @scrutinizer ignore-unused */ $billingData, $shippingData, $paymentInformation): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $paymentInformation is not used and could be removed. ( Ignorable by Annotation )

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

73
    private function magicallyCreateCustomerId($billingData, $shippingData, /** @scrutinizer ignore-unused */ $paymentInformation): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
    {
75
        return self::FAKE_CUSTOMER_ID;
76
    }
77
}
78