|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Wubinworks. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Wubinworks\DisableChangeEmail\Helper; |
| 9 | + |
| 10 | +use Magento\Framework\App\Area; |
| 11 | +use Magento\Framework\App\State as AppState; |
| 12 | +use Magento\Authorization\Model\UserContextInterface; |
| 13 | +use Magento\Framework\Message\ManagerInterface as MessageManagerInterface; |
| 14 | +use Magento\Framework\Exception\LocalizedException; |
| 15 | +use Magento\Framework\Event\ManagerInterface as EventManagerInterface; |
| 16 | +use Magento\Framework\Phrase; |
| 17 | + |
| 18 | +/** |
| 19 | + * System helper |
| 20 | + */ |
| 21 | +class System extends \Magento\Framework\App\Helper\AbstractHelper |
| 22 | +{ |
| 23 | + public const XML_PATH_CUSTOMER_ACCOUNT_INFORMATION_DISABLE_CHANGE_EMAIL |
| 24 | + = 'customer/account_information/disable_change_email'; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var AppState |
| 28 | + */ |
| 29 | + protected $appState; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var UserContextInterface |
| 33 | + */ |
| 34 | + protected $userContext; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var MessageManagerInterface |
| 38 | + */ |
| 39 | + protected $messageManager; |
| 40 | + |
| 41 | + /** |
| 42 | + * Application Event Dispatcher |
| 43 | + * |
| 44 | + * @var EventManagerInterface |
| 45 | + */ |
| 46 | + protected $eventManager; |
| 47 | + |
| 48 | + /** |
| 49 | + * Constructor |
| 50 | + * |
| 51 | + * @param AppState $appState |
| 52 | + * @param UserContextInterface $userContext |
| 53 | + * @param MessageManagerInterface $messageManager |
| 54 | + * @param EventManagerInterface $eventManager |
| 55 | + * @param \Magento\Framework\App\Helper\Context $context |
| 56 | + */ |
| 57 | + public function __construct( |
| 58 | + AppState $appState, |
| 59 | + UserContextInterface $userContext, |
| 60 | + MessageManagerInterface $messageManager, |
| 61 | + EventManagerInterface $eventManager, |
| 62 | + \Magento\Framework\App\Helper\Context $context |
| 63 | + ) { |
| 64 | + parent::__construct($context); |
| 65 | + $this->appState = $appState; |
| 66 | + $this->userContext= $userContext; |
| 67 | + $this->messageManager = $messageManager; |
| 68 | + $this->eventManager = $eventManager; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get current store system configuration value |
| 73 | + * |
| 74 | + * @param string $path |
| 75 | + * @param string $scopeType |
| 76 | + * @param null|int|string $scopeCode |
| 77 | + * @return mixed |
| 78 | + */ |
| 79 | + public function getConfig($path, $scopeType = \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $scopeCode = null) |
| 80 | + { |
| 81 | + return $this->scopeConfig->getValue( |
| 82 | + $path, |
| 83 | + $scopeType, |
| 84 | + $scopeCode |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Get current area code. |
| 90 | + * |
| 91 | + * @return string |
| 92 | + */ |
| 93 | + public function getArea(): string |
| 94 | + { |
| 95 | + try { |
| 96 | + $areaCode = $this->appState->getAreaCode(); |
| 97 | + } catch (LocalizedException $e) { |
| 98 | + $areaCode = 'unknown'; |
| 99 | + } |
| 100 | + |
| 101 | + return $areaCode; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Is admin or integration context |
| 106 | + * |
| 107 | + * @return bool |
| 108 | + */ |
| 109 | + public function isAdminOrIntegration(): bool |
| 110 | + { |
| 111 | + return in_array( |
| 112 | + $this->userContext->getUserType(), |
| 113 | + [UserContextInterface::USER_TYPE_INTEGRATION, UserContextInterface::USER_TYPE_ADMIN] |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Is in webapi area |
| 119 | + * |
| 120 | + * @return bool |
| 121 | + */ |
| 122 | + public function isWebapi(): bool |
| 123 | + { |
| 124 | + return in_array( |
| 125 | + $this->getArea(), |
| 126 | + [Area::AREA_WEBAPI_REST, Area::AREA_WEBAPI_SOAP, Area::AREA_GRAPHQL] |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Get current full action name |
| 132 | + * |
| 133 | + * @param string $delimiter |
| 134 | + * @return string|null |
| 135 | + */ |
| 136 | + public function getFullActionName(string $delimiter = '/') |
| 137 | + { |
| 138 | + if ($this->getArea() === Area::AREA_FRONTEND |
| 139 | + || $this->getArea() === Area::AREA_ADMINHTML) { |
| 140 | + return $this->_request->getFullActionName($delimiter); |
| 141 | + } |
| 142 | + |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Get message manager |
| 148 | + * |
| 149 | + * @return MessageManagerInterface |
| 150 | + */ |
| 151 | + public function getMessageManager(): MessageManagerInterface |
| 152 | + { |
| 153 | + return $this->messageManager; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Get user context object |
| 158 | + * |
| 159 | + * @return UserContextInterface |
| 160 | + */ |
| 161 | + public function getUserContextObject(): UserContextInterface |
| 162 | + { |
| 163 | + return $this->userContext; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Get event manager |
| 168 | + * |
| 169 | + * @return EventManagerInterface |
| 170 | + */ |
| 171 | + public function getEventManager(): EventManagerInterface |
| 172 | + { |
| 173 | + return $this->eventManager; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Build url |
| 178 | + * |
| 179 | + * @param string $route |
| 180 | + * @param array $params |
| 181 | + * @return string |
| 182 | + */ |
| 183 | + public function getUrl(string $route, $params = []) |
| 184 | + { |
| 185 | + return $this->_getUrl($route, $params); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Is change email disabled |
| 190 | + * |
| 191 | + * @return bool |
| 192 | + */ |
| 193 | + public function isChangeEmailDisabled(): bool |
| 194 | + { |
| 195 | + return (bool)$this->getConfig(self::XML_PATH_CUSTOMER_ACCOUNT_INFORMATION_DISABLE_CHANGE_EMAIL); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Get change email ertor phrase |
| 200 | + * |
| 201 | + * @return Phrase |
| 202 | + */ |
| 203 | + public function getChangeEmailErrorPhrase(): Phrase |
| 204 | + { |
| 205 | + return new Phrase('You cannot change email address.'); |
| 206 | + } |
| 207 | +} |
0 commit comments