Passed
Push — master ( 8bfa1c...b6a090 )
by Fabian
02:35
created

CartBuilder::withSimpleProduct()   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
    /**
55
     * Lower-level API to support arbitary products
56
     *
57
     * @param $sku
58
     * @param int $qty
59
     * @param $request
60
     * @return CartBuilder
61
     */
62
    public function withProductRequest($sku, $qty = 1, $request = []) : CartBuilder
63
    {
64
        $result = clone $this;
65
        $requestInfo = array_merge(['qty'=> $qty], $request);
66
        $result->addToCartRequests[$sku][] = new DataObject($requestInfo);
67
        return $result;
68
    }
69
70
    public function build() : Cart
71
    {
72
        foreach ($this->addToCartRequests as $sku => $requests) {
73
            /** @var $product \Magento\Catalog\Model\Product */
74
            $product = $this->productRepository->get($sku);
75
            foreach ($requests as $requestInfo) {
76
                $this->cart->addProduct($product, $requestInfo);
77
            }
78
        }
79
        $this->cart->save();
80
        return $this->cart;
81
    }
82
}
83