CartBuilder   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 27
c 3
b 0
f 0
dl 0
loc 78
ccs 26
cts 30
cp 0.8667
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A forCurrentSession() 0 6 1
A withReservedOrderId() 0 5 1
A build() 0 11 3
A withProductRequest() 0 6 1
A withSimpleProduct() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TddWizard\Fixtures\Checkout;
5
6
use Magento\Catalog\Api\ProductRepositoryInterface;
7
use Magento\Catalog\Model\Product;
8
use Magento\Checkout\Model\Cart;
9
use Magento\Framework\DataObject;
10
use Magento\Framework\Exception\LocalizedException;
11
use Magento\TestFramework\Helper\Bootstrap;
0 ignored issues
show
Bug introduced by
The type Magento\TestFramework\Helper\Bootstrap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class CartBuilder
14
{
15
    /**
16
     * @var ProductRepositoryInterface
17
     */
18
    private $productRepository;
19
20
    /**
21
     * @var Cart
22
     */
23
    private $cart;
24
25
    /**
26
     * @var DataObject[][] Array in the form [sku => [buyRequest]] (multiple requests per sku are possible)
27
     */
28
    private $addToCartRequests;
29
30 17
    final public function __construct(ProductRepositoryInterface $productRepository, Cart $cart)
31
    {
32 17
        $this->productRepository = $productRepository;
33 17
        $this->cart = $cart;
34 17
        $this->addToCartRequests = [];
35 17
    }
36
37 17
    public static function forCurrentSession(): CartBuilder
38
    {
39 17
        $objectManager = Bootstrap::getObjectManager();
40 17
        return new static(
41 17
            $objectManager->create(ProductRepositoryInterface::class),
42 17
            $objectManager->create(Cart::class)
43
        );
44
    }
45
46 16
    public function withSimpleProduct(string $sku, float $qty = 1): CartBuilder
47
    {
48 16
        $result = clone $this;
49 16
        $result->addToCartRequests[$sku][] = new DataObject(['qty' => $qty]);
50 16
        return $result;
51
    }
52
53
    public function withReservedOrderId(string $orderId): CartBuilder
54
    {
55
        $result = clone $this;
56
        $result->cart->getQuote()->setReservedOrderId($orderId);
57
        return $result;
58
    }
59
60
    /**
61
     * Lower-level API to support arbitrary products
62
     *
63
     * @param string $sku
64
     * @param int $qty
65
     * @param mixed[] $request
66
     * @return CartBuilder
67
     */
68 1
    public function withProductRequest($sku, $qty = 1, $request = []): CartBuilder
69
    {
70 1
        $result = clone $this;
71 1
        $requestInfo = array_merge(['qty' => $qty], $request);
72 1
        $result->addToCartRequests[$sku][] = new DataObject($requestInfo);
73 1
        return $result;
74
    }
75
76
    /**
77
     * @return Cart
78
     * @throws LocalizedException
79
     */
80 17
    public function build(): Cart
81
    {
82 17
        foreach ($this->addToCartRequests as $sku => $requests) {
83
            /** @var Product $product */
84 17
            $product = $this->productRepository->get($sku);
85 17
            foreach ($requests as $requestInfo) {
86 17
                $this->cart->addProduct($product, $requestInfo);
87
            }
88
        }
89 17
        $this->cart->save();
90 17
        return $this->cart;
91
    }
92
}
93