forked from Brille24/SyliusCustomOptionsPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddingTypesToAdjustmentClearerPass.php
More file actions
47 lines (36 loc) · 1.96 KB
/
AddingTypesToAdjustmentClearerPass.php
File metadata and controls
47 lines (36 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
declare(strict_types=1);
namespace Brille24\SyliusCustomerOptionsPlugin\CompilerPass;
use Brille24\SyliusCustomerOptionsPlugin\Services\CustomerOptionRecalculator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
final class AddingTypesToAdjustmentClearerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
// Gets the definition of the OrderAdjustmentClearer
$clearerDefinition = $container->getDefinition('sylius.order_processing.order_adjustments_clearer');
// Getting the new list of adjustment types to clear
$listOfAdjustmentsToClear = $clearerDefinition->getArgument(0);
// If the argument is a string then it's the name of the parameter where the services are configured
// We then just need to modify the parameter.
if (is_string($listOfAdjustmentsToClear)) {
$this->addClearerToContainerParameter($container, $listOfAdjustmentsToClear);
return;
}
// BC: For Symfony < 6 (modifing the service directly)
$listOfAdjustmentsToClear[] = CustomerOptionRecalculator::CUSTOMER_OPTION_ADJUSTMENT;
// Setting the new list as the new definition
$clearerDefinition->setArgument(0, $listOfAdjustmentsToClear);
}
private function addClearerToContainerParameter(ContainerBuilder $container, string $parameterName): void
{
if (1 !== preg_match('/^%(.*)%$/', $parameterName, $matches)) {
throw new \RuntimeException(sprintf('Could not match placeholders in parameter name %s', $parameterName));
}
$parameterName = $matches[1];
$listOfAdjustmentsToClear = $container->getParameter($parameterName);
$listOfAdjustmentsToClear[] = CustomerOptionRecalculator::CUSTOMER_OPTION_ADJUSTMENT;
$container->setParameter($parameterName, $listOfAdjustmentsToClear);
}
}