Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Document.php
Go to the documentation of this file.
1 <?php
7 
17 
22 {
26  private static $genderAttributeCode = 'gender';
27 
31  private static $groupAttributeCode = 'group_id';
32 
36  private static $websiteAttributeCode = 'website_id';
37 
41  private static $websiteIdAttributeCode = 'original_website_id';
42 
46  private static $confirmationAttributeCode = 'confirmation';
47 
51  private static $accountLockAttributeCode = 'lock_expires';
52 
56  private $customerMetadata;
57 
61  private $groupRepository;
62 
66  private $storeManager;
67 
71  private $scopeConfig;
72 
81  public function __construct(
83  GroupRepositoryInterface $groupRepository,
84  CustomerMetadataInterface $customerMetadata,
85  StoreManagerInterface $storeManager,
86  ScopeConfigInterface $scopeConfig = null
87  ) {
88  parent::__construct($attributeValueFactory);
89  $this->customerMetadata = $customerMetadata;
90  $this->groupRepository = $groupRepository;
91  $this->storeManager = $storeManager;
92  $this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->create(ScopeConfigInterface::class);
93  }
94 
99  {
100  switch ($attributeCode) {
101  case self::$genderAttributeCode:
102  $this->setGenderValue();
103  break;
104  case self::$groupAttributeCode:
105  $this->setCustomerGroupValue();
106  break;
107  case self::$websiteAttributeCode:
108  $this->setWebsiteValue();
109  break;
110  case self::$confirmationAttributeCode:
111  $this->setConfirmationValue();
112  break;
113  case self::$accountLockAttributeCode:
114  $this->setAccountLockValue();
115  break;
116  }
117  return parent::getCustomAttribute($attributeCode);
118  }
119 
125  private function setGenderValue()
126  {
127  $value = $this->getData(self::$genderAttributeCode);
128 
129  if (!$value) {
130  $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
131  return;
132  }
133 
134  try {
135  $attributeMetadata = $this->customerMetadata->getAttributeMetadata(self::$genderAttributeCode);
136  $option = $attributeMetadata->getOptions()[$value];
137  $this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel());
138  } catch (NoSuchEntityException $e) {
139  $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
140  }
141  }
142 
148  private function setCustomerGroupValue()
149  {
150  $value = $this->getData(self::$groupAttributeCode);
151  try {
152  $group = $this->groupRepository->getById($value);
153  $this->setCustomAttribute(self::$groupAttributeCode, $group->getCode());
154  } catch (NoSuchEntityException $e) {
155  $this->setCustomAttribute(self::$groupAttributeCode, 'N/A');
156  }
157  }
158 
164  private function setWebsiteValue()
165  {
166  $value = $this->getData(self::$websiteAttributeCode);
167  $list = $this->storeManager->getWebsites();
168  $this->setCustomAttribute(self::$websiteAttributeCode, $list[$value]->getName());
169  $this->setCustomAttribute(self::$websiteIdAttributeCode, $value);
170  }
171 
177  private function setConfirmationValue()
178  {
179  $value = $this->getData(self::$confirmationAttributeCode);
180  $websiteId = $this->getData(self::$websiteIdAttributeCode) ?: $this->getData(self::$websiteAttributeCode);
181  $isConfirmationRequired = (bool)$this->scopeConfig->getValue(
182  AccountManagement::XML_PATH_IS_CONFIRM,
183  ScopeInterface::SCOPE_WEBSITES,
184  $websiteId
185  );
186 
187  $valueText = __('Confirmation Not Required');
188  if ($isConfirmationRequired) {
189  $valueText = $value === null ? __('Confirmed') : __('Confirmation Required');
190  }
191 
192  $this->setCustomAttribute(self::$confirmationAttributeCode, $valueText);
193  }
194 
200  private function setAccountLockValue()
201  {
202  $value = $this->getDataByPath(self::$accountLockAttributeCode);
203 
204  $valueText = __('Unlocked');
205  if ($value !== null) {
206  $lockExpires = new \DateTime($value);
207  if ($lockExpires > new \DateTime()) {
208  $valueText = __('Locked');
209  }
210  }
211 
212  $this->setCustomAttribute(self::$accountLockAttributeCode, $valueText);
213  }
214 }
$groupRepository
getData($key='', $index=null)
Definition: DataObject.php:119
$group
Definition: sections.phtml:16
$storeManager
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
__construct(AttributeValueFactory $attributeValueFactory, GroupRepositoryInterface $groupRepository, CustomerMetadataInterface $customerMetadata, StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig=null)
Definition: Document.php:81