|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Setono\SyliusStrandsPlugin\Twig; |
|
6
|
|
|
|
|
7
|
|
|
use Setono\SyliusStrandsPlugin\Resolver\ItemCodeResolverInterface; |
|
8
|
|
|
use Setono\SyliusStrandsPlugin\Widget\Widget; |
|
9
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
|
10
|
|
|
use Sylius\Component\Order\Context\CartContextInterface; |
|
11
|
|
|
use Twig\Extension\RuntimeExtensionInterface; |
|
12
|
|
|
|
|
13
|
|
|
final class Runtime implements RuntimeExtensionInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var ItemCodeResolverInterface */ |
|
16
|
|
|
private $itemCodeResolver; |
|
17
|
|
|
|
|
18
|
|
|
/** @var CartContextInterface */ |
|
19
|
|
|
private $cartContext; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(ItemCodeResolverInterface $itemCodeResolver, CartContextInterface $cartContext) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->itemCodeResolver = $itemCodeResolver; |
|
24
|
|
|
$this->cartContext = $cartContext; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function itemCode(OrderItemInterface $item): string |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->itemCodeResolver->resolve($item); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function widget(string $template): Widget |
|
33
|
|
|
{ |
|
34
|
|
|
return new Widget($template); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function addItems(Widget $widget, iterable $items): Widget |
|
38
|
|
|
{ |
|
39
|
|
|
foreach ($items as $item) { |
|
40
|
|
|
if ($item instanceof OrderItemInterface) { |
|
41
|
|
|
$widget->addItem($this->itemCodeResolver->resolve($item)); |
|
42
|
|
|
} else { |
|
43
|
|
|
$widget->addItem($item); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $widget; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function addCartItems(Widget $widget): Widget |
|
51
|
|
|
{ |
|
52
|
|
|
$cart = $this->cartContext->getCart(); |
|
53
|
|
|
|
|
54
|
|
|
if (count($cart->getItems()) === 0) { |
|
55
|
|
|
return $widget; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->addItems($widget, $cart->getItems()); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|