Skip to content

Commit

Permalink
Merge pull request #22 from tuyennn/feature/fix-command-website-option
Browse files Browse the repository at this point in the history
[fix] Remove exception while website option on command doesn't stand …
  • Loading branch information
tuyennn authored Dec 18, 2024
2 parents 507a13f + b3dc952 commit 106e5cf
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions Command/CustomerChangePasswordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -49,20 +50,20 @@ 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;
}
Expand Down Expand Up @@ -96,10 +97,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('<error>' . $e->getMessage() . '</error>');
return Command::FAILURE;
}

return Command::SUCCESS;
}

/**
Expand Down Expand Up @@ -133,7 +141,7 @@ private function getWebsiteCode(): string
}

/**
* Get website Id by code
* Get website id by code
*
* @param string $code
* @return int
Expand All @@ -158,12 +166,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()));
}
Expand Down

0 comments on commit 106e5cf

Please sign in to comment.