From a1781f95b20c4e04f2ec97f2cc2077b0b33022a2 Mon Sep 17 00:00:00 2001 From: tuyennn Date: Wed, 18 Dec 2024 14:34:29 +0700 Subject: [PATCH 1/3] [fix] Remove exception while website option on command doesn't stand for optional. --- Command/CustomerChangePasswordCommand.php | 39 +++++++++++++---------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/Command/CustomerChangePasswordCommand.php b/Command/CustomerChangePasswordCommand.php index 9ed9f10..8191f64 100644 --- a/Command/CustomerChangePasswordCommand.php +++ b/Command/CustomerChangePasswordCommand.php @@ -5,6 +5,7 @@ use Magento\Customer\Model\Customer; use Magento\Customer\Model\CustomerFactory; +use Magento\Customer\Model\CustomerRegistry; use Magento\Customer\Model\ResourceModel\Customer as CustomerResource; use Magento\Framework\App\Area; use Magento\Framework\App\State as AppState; @@ -22,14 +23,14 @@ class CustomerChangePasswordCommand extends Command { /** - * @var CustomerFactory + * @var CustomerResource */ - private $customerFactory; + private $customerResource; /** - * @var CustomerResource + * @var CustomerRegistry */ - private $customerResource; + private $customerRegistry; /** * @var StoreManagerInterface @@ -49,20 +50,21 @@ class CustomerChangePasswordCommand extends Command /** * CustomerChangePasswordCommand constructor * - * @param CustomerFactory $customerFactory * @param StoreManagerInterface $storeManager * @param CustomerResource $resource + * @param CustomerRegistry $customerRegistry * @param AppState $state */ public function __construct( - CustomerFactory $customerFactory, StoreManagerInterface $storeManager, CustomerResource $resource, + CustomerRegistry $customerRegistry, AppState $state ) { parent::__construct(); $this->customerFactory = $customerFactory; $this->customerResource = $resource; + $this->customerRegistry = $customerRegistry; $this->storeManager = $storeManager; $this->appState = $state; } @@ -96,10 +98,17 @@ protected function execute(InputInterface $input, OutputInterface $output) } catch (LocalizedException $exception) { } - $customer = $this->getCustomerByEmail($this->getEmail()); - $customer->setPassword($this->getPassword()); - $this->customerResource->save($customer); - $output->writeln(sprintf('Updated password for customer "%s".', $this->getEmail())); + try { + $customer = $this->getCustomerByEmail($this->getEmail()); + $customer->setPassword($this->getPassword()); + $this->customerResource->save($customer); + $output->writeln(sprintf('Updated password for customer "%s".', $this->getEmail())); + } catch (\Exception $e) { + $output->writeln('' . $e->getMessage() . ''); + return Command::FAILURE; + } + + return Command::SUCCESS; } /** @@ -133,7 +142,7 @@ private function getWebsiteCode(): string } /** - * Get website Id by code + * Get website I'd by code * * @param string $code * @return int @@ -158,12 +167,8 @@ private function getWebsiteIdByCode(string $code): int */ private function getCustomerByEmail(string $email): Customer { - $customer = $this->customerFactory->create(); - if ($this->getWebsiteCode()) { - $websiteId = $this->getWebsiteIdByCode($this->getWebsiteCode()); - $customer->setWebsiteId($websiteId); - } - $this->customerResource->loadByEmail($customer, $email); + $websiteId = $this->getWebsiteCode() ? $this->getWebsiteIdByCode($this->getWebsiteCode()) : null; + $customer = $this->customerRegistry->retrieveByEmail($email, $websiteId); if (!$customer->getId()) { throw new \InvalidArgumentException(sprintf('No customer with email "%s" found.', $this->getEmail())); } From 823cc769f15c4da4738e765b86f6e829d6b2c8c8 Mon Sep 17 00:00:00 2001 From: tuyennn Date: Wed, 18 Dec 2024 14:42:06 +0700 Subject: [PATCH 2/3] [fix] typo --- Command/CustomerChangePasswordCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Command/CustomerChangePasswordCommand.php b/Command/CustomerChangePasswordCommand.php index 8191f64..b5c340f 100644 --- a/Command/CustomerChangePasswordCommand.php +++ b/Command/CustomerChangePasswordCommand.php @@ -142,7 +142,7 @@ private function getWebsiteCode(): string } /** - * Get website I'd by code + * Get website id by code * * @param string $code * @return int From b3dc9523dafce225804929070d86a89ecb59e186 Mon Sep 17 00:00:00 2001 From: tuyennn Date: Wed, 18 Dec 2024 14:46:14 +0700 Subject: [PATCH 3/3] [fix] Undefined variable $customerFactory in Command/CustomerChangePasswordCommand.php on line 65 --- Command/CustomerChangePasswordCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Command/CustomerChangePasswordCommand.php b/Command/CustomerChangePasswordCommand.php index b5c340f..4534e14 100644 --- a/Command/CustomerChangePasswordCommand.php +++ b/Command/CustomerChangePasswordCommand.php @@ -62,7 +62,6 @@ public function __construct( AppState $state ) { parent::__construct(); - $this->customerFactory = $customerFactory; $this->customerResource = $resource; $this->customerRegistry = $customerRegistry; $this->storeManager = $storeManager;