Passed
Push — master ( 6d5a60...d36518 )
by Fabian
04:42
created

CartBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace TddWizard\Fixtures\Checkout;
4
5
use Magento\Catalog\Api\ProductRepositoryInterface;
6
use Magento\Catalog\Model\Product;
7
use Magento\Checkout\Model\Cart;
8
use Magento\Framework\DataObject;
9
use Magento\Framework\ObjectManagerInterface;
10
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...
11
12
class CartBuilder
13
{
14
    /**
15
     * @var ProductRepositoryInterface
16
     */
17
    private $productRepository;
18
19
    /**
20
     * @var Cart
21
     */
22
    private $cart;
23
24
    /**
25
     * @var DataObject[][] Array in the form [sku => [buyRequest]] (multiple requests per sku are possible)
26
     */
27
    private $addToCartRequests;
28
29
    public function __construct(ProductRepositoryInterface $productRepository, Cart $cart)
30
    {
31
        $this->productRepository = $productRepository;
32
        $this->cart = $cart;
33
        $this->addToCartRequests = [];
34
    }
35
36
    public static function forCurrentSession(ObjectManagerInterface $objectManager = null)
37
    {
38
        if ($objectManager === null) {
39
            $objectManager = Bootstrap::getObjectManager();
40
        }
41
        return new static(
42
            $objectManager->create(ProductRepositoryInterface::class),
43
            $objectManager->create(Cart::class)
44
        );
45
    }
46
47
    public function withSimpleProduct($sku, $qty = 1) : CartBuilder
48
    {
49
        $result = clone $this;
50
        $result->addToCartRequests[$sku][] = new DataObject(['qty' => $qty]);
51
        return $result;
52
    }
53
54
    public function withReservedOrderId($orderId) : CartBuilder
55
    {
56
        $result = clone $this;
57
        $result->cart->getQuote()->setReservedOrderId($orderId);
58
        return $result;
59
    }
60
61
    /**
62
     * Lower-level API to support arbitary products
63
     *
64
     * @param $sku
65
     * @param int $qty
66
     * @param $request
67
     * @return CartBuilder
68
     */
69
    public function withProductRequest($sku, $qty = 1, $request = []) : CartBuilder
70
    {
71
        $result = clone $this;
72
        $requestInfo = array_merge(['qty'=> $qty], $request);
73
        $result->addToCartRequests[$sku][] = new DataObject($requestInfo);
74
        return $result;
75
    }
76
77
    public function build() : Cart
78
    {
79
        foreach ($this->addToCartRequests as $sku => $requests) {
80
            /** @var $product \Magento\Catalog\Model\Product */
81
            $product = $this->productRepository->get($sku);
82
            foreach ($requests as $requestInfo) {
83
                $this->cart->addProduct($product, $requestInfo);
84
            }
85
        }
86
        $this->cart->save();
87
        return $this->cart;
88
    }
89
}
90