Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerGroup.php
Go to the documentation of this file.
1 <?php
7 
17 
19 {
23  private $updateIndex;
24 
28  private $tableMaintainer;
29 
35  private $dimensionFactory;
36 
40  private $dimensionModeConfiguration;
41 
45  private $websiteDimensionProvider;
46 
54  public function __construct(
55  UpdateIndexInterface $updateIndex,
56  TableMaintainer $tableMaintainer,
57  DimensionFactory $dimensionFactory,
58  DimensionModeConfiguration $dimensionModeConfiguration,
59  WebsiteDimensionProvider $websiteDimensionProvider
60  ) {
61  $this->updateIndex = $updateIndex;
62  $this->tableMaintainer = $tableMaintainer;
63  $this->dimensionFactory = $dimensionFactory;
64  $this->dimensionModeConfiguration = $dimensionModeConfiguration;
65  $this->websiteDimensionProvider = $websiteDimensionProvider;
66  }
67 
78  public function aroundSave(
79  GroupRepositoryInterface $subject,
80  \Closure $proceed,
82  ) {
83  $isGroupNew = !$group->getId();
84  $group = $proceed($group);
85  if ($isGroupNew) {
86  foreach ($this->getAffectedDimensions((string)$group->getId()) as $dimensions) {
87  $this->tableMaintainer->createTablesForDimensions($dimensions);
88  }
89  }
90  $this->updateIndex->update($group, $isGroupNew);
91  return $group;
92  }
93 
104  public function afterDeleteById(GroupRepositoryInterface $subject, bool $result, string $groupId)
105  {
106  foreach ($this->getAffectedDimensions($groupId) as $dimensions) {
107  $this->tableMaintainer->dropTablesForDimensions($dimensions);
108  }
109 
110  return $result;
111  }
112 
119  private function getAffectedDimensions(string $groupId): array
120  {
121  $currentDimensions = $this->dimensionModeConfiguration->getDimensionConfiguration();
122  // do not return dimensions if Customer Group dimension is not present in configuration
123  if (!in_array(CustomerGroupDimensionProvider::DIMENSION_NAME, $currentDimensions, true)) {
124  return [];
125  }
126  $customerGroupDimension = $this->dimensionFactory->create(
127  CustomerGroupDimensionProvider::DIMENSION_NAME,
128  $groupId
129  );
130 
131  $dimensions = [];
132  if (in_array(WebsiteDimensionProvider::DIMENSION_NAME, $currentDimensions, true)) {
133  foreach ($this->websiteDimensionProvider as $websiteDimension) {
134  $dimensions[] = [
135  $customerGroupDimension,
136  $websiteDimension
137  ];
138  }
139  } else {
140  $dimensions[] = [$customerGroupDimension];
141  }
142 
143  return $dimensions;
144  }
145 }
__construct(UpdateIndexInterface $updateIndex, TableMaintainer $tableMaintainer, DimensionFactory $dimensionFactory, DimensionModeConfiguration $dimensionModeConfiguration, WebsiteDimensionProvider $websiteDimensionProvider)
afterDeleteById(GroupRepositoryInterface $subject, bool $result, string $groupId)
$group
Definition: sections.phtml:16
aroundSave(GroupRepositoryInterface $subject, \Closure $proceed, GroupInterface $group)