<?php declare(strict_types=1);
namespace Acris\Tax\Core\Checkout\Cart;
use Shopware\Core\Checkout\Cart\CachedRuleLoader;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Content\Rule\RuleCollection;
use Shopware\Core\Framework\Context;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
class TaxRuleLoader
{
/**
* @var RuleCollection|null
*/
private $rules;
/**
* @var TagAwareAdapterInterface
*/
private $cache;
/**
* @var CachedRuleLoader
*/
private $ruleLoader;
public function __construct(
TagAwareAdapterInterface $cache,
CachedRuleLoader $ruleLoader
) {
$this->cache = $cache;
$this->ruleLoader = $ruleLoader;
$this->rules = null;
}
public function loadRulesIdsForContext(SalesChannelContext $salesChannelContext): array
{
$cart = new Cart($salesChannelContext->getSalesChannel()->getTypeId(), $salesChannelContext->getToken());
return $this->loadRules($salesChannelContext->getContext())->filterMatchingRules($cart, $salesChannelContext)->getIds();
}
private function loadRules(Context $context): RuleCollection
{
if ($this->rules !== null) {
return $this->rules;
}
return $this->rules = $this->ruleLoader->load($context);
}
}