Passed
Push — trunk ( d9cd06...1ca626 )
by Christian
16:06 queued 03:18
created

FlowRuleScopeBuilder::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Flow\Rule;
4
5
use Shopware\Core\Checkout\Cart\CartBehavior;
6
use Shopware\Core\Checkout\Cart\CartDataCollectorInterface;
7
use Shopware\Core\Checkout\Cart\Delivery\DeliveryBuilder;
8
use Shopware\Core\Checkout\Cart\Order\OrderConverter;
9
use Shopware\Core\Checkout\Order\OrderEntity;
10
use Shopware\Core\Framework\Context;
11
use Symfony\Contracts\Service\ResetInterface;
12
13
/**
14
 * @internal
15
 */
16
class FlowRuleScopeBuilder implements ResetInterface
17
{
18
    private OrderConverter $orderConverter;
19
20
    private DeliveryBuilder $deliveryBuilder;
21
22
    /**
23
     * @var iterable<CartDataCollectorInterface>
24
     */
25
    private iterable $collectors;
26
27
    /**
28
     * @var array<string, FlowRuleScope>
29
     */
30
    private array $scopes = [];
31
32
    /**
33
     * @param iterable<CartDataCollectorInterface> $collectors
34
     */
35
    public function __construct(OrderConverter $orderConverter, DeliveryBuilder $deliveryBuilder, iterable $collectors)
36
    {
37
        $this->orderConverter = $orderConverter;
38
        $this->deliveryBuilder = $deliveryBuilder;
39
        $this->collectors = $collectors;
40
    }
41
42
    public function reset(): void
43
    {
44
        $this->scopes = [];
45
    }
46
47
    public function build(OrderEntity $order, Context $context): FlowRuleScope
48
    {
49
        if (\array_key_exists($order->getId(), $this->scopes)) {
50
            return $this->scopes[$order->getId()];
51
        }
52
53
        $context = $this->orderConverter->assembleSalesChannelContext($order, $context);
54
        $cart = $this->orderConverter->convertToCart($order, $context->getContext());
55
        $behavior = new CartBehavior($context->getPermissions());
56
57
        foreach ($this->collectors as $collector) {
58
            $collector->collect($cart->getData(), $cart, $context, $behavior);
59
        }
60
61
        $cart->setDeliveries(
62
            $this->deliveryBuilder->build($cart, $cart->getData(), $context, $behavior)
63
        );
64
65
        return $this->scopes[$order->getId()] = new FlowRuleScope($order, $cart, $context);
66
    }
67
}
68