Completed
Push — master ( 7d6b3f...2df636 )
by Fabian
14s queued 10s
created

CartBuilder::withSimpleProduct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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 16
    final public function __construct(ProductRepositoryInterface $productRepository, Cart $cart)
31
    {
32 16
        $this->productRepository = $productRepository;
33 16
        $this->cart = $cart;
34 16
        $this->addToCartRequests = [];
35 16
    }
36
37 16
    public static function forCurrentSession(): CartBuilder
38
    {
39 16
        $objectManager = Bootstrap::getObjectManager();
40 16
        return new static(
41 16
            $objectManager->create(ProductRepositoryInterface::class),
42 16
            $objectManager->create(Cart::class)
43
        );
44
    }
45
46 15
    public function withSimpleProduct(string $sku, float $qty = 1): CartBuilder
47
    {
48 15
        $result = clone $this;
49 15
        $result->addToCartRequests[$sku][] = new DataObject(['qty' => $qty]);
50 15
        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 16
    public function build(): Cart
81
    {
82 16
        foreach ($this->addToCartRequests as $sku => $requests) {
83
            /** @var Product $product */
84 16
            $product = $this->productRepository->get($sku);
85 16
            foreach ($requests as $requestInfo) {
86 16
                $this->cart->addProduct($product, $requestInfo);
87
            }
88
        }
89 16
        $this->cart->save();
90 16
        return $this->cart;
91
    }
92
}
93