Skip to content

Commit

Permalink
Added a couple of customer related commands
Browse files Browse the repository at this point in the history
  • Loading branch information
fballiano committed Sep 1, 2024
1 parent c6aaa97 commit 25c7f41
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/MahoCLI/Commands/AdminUserChangepassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#[AsCommand(
name: 'admin:user:change-password',
description: 'List all admin users'
description: 'Change password of an admin users'
)]
class AdminUserChangepassword extends BaseMahoCommand
{
Expand Down
94 changes: 94 additions & 0 deletions lib/MahoCLI/Commands/CustomerChangepassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace MahoCLI\Commands;

use Mage;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

#[AsCommand(
name: 'customer:change-password',
description: 'Change password of a customers'
)]
class CustomerChangepassword extends BaseMahoCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->initMaho();

/** @var QuestionHelper $questionHelper */
$questionHelper = $this->getHelper('question');

$question = new Question('Email: ', '');
$email = $questionHelper->ask($input, $output, $question);
$email = trim($email);
if (!strlen($email)) {
$output->writeln('<error>Email cannot be empty</error>');
return Command::INVALID;
}

$question = new Question('New password: ', '');
$password = $questionHelper->ask($input, $output, $question);
$password = trim($password);
if (!strlen($password)) {
$output->writeln('<error>New password cannot be empty</error>');
return Command::INVALID;
}

$customerAttributes = ['entity_id', 'website_id', 'email', 'firstname', 'lastname'];
$customers = Mage::getResourceModel('customer/customer_collection')
->addAttributeToSelect($customerAttributes)
->addAttributeToFilter('email', ['eq' => $email]);

if ($customers->getSize() == 0) {
$output->writeln("<error>Customer not found.</error>");
return Command::FAILURE;
}

if ($customers->getSize() == 1) {
foreach ($customers as $customer) {
$customer->setPassword($password);
$customer->save();
$output->writeln("<info>Customer $email deleted.</info>");
return Command::SUCCESS;
}
}

$output->writeln('');
$output->writeln("<comment>More than one customer matches your search:</comment>");
$table = new Table($output);
$table->setHeaders($customerAttributes);

foreach ($customers as $customer) {
$table->addRow([
$customer->getEntityId(),
$customer->getWebsiteId(),
$customer->getEmail(),
$customer->getFirstname(),
$customer->getLastname()
]);
}
$table->render();

$question = new Question('Type the ID of the customer you wish to change the password for: ', '');
$customerId = $questionHelper->ask($input, $output, $question);
$customerId = trim($customerId);

$customer = Mage::getModel('customer/customer')
->load($customerId);
if (!$customer->getId()) {
$output->writeln("<error>Customer not found.</error>");
return Command::FAILURE;
}

$customer->setPassword($password);
$customer->save();
$output->writeln("<info>Customer $email deleted.</info>");
return Command::SUCCESS;
}
}
90 changes: 90 additions & 0 deletions lib/MahoCLI/Commands/CustomerCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace MahoCLI\Commands;

use Mage;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;

#[AsCommand(
name: 'customer:create',
description: 'Create a customer'
)]
class CustomerCreate extends BaseMahoCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->initMaho();

/** @var QuestionHelper $questionHelper */
$questionHelper = $this->getHelper('question');

$question = new Question('Email: ', '');
$email = $questionHelper->ask($input, $output, $question);
$email = trim($email);
if (!strlen($email)) {
$output->writeln('<error>Email cannot be empty</error>');
return Command::INVALID;
}

$question = new Question('Password: ', '');
$password = $questionHelper->ask($input, $output, $question);
$password = trim($password);
if (!strlen($password)) {
$output->writeln('<error>Password cannot be empty</error>');
return Command::INVALID;
}

$question = new Question('Firstname: ', '');
$firstname = $questionHelper->ask($input, $output, $question);
$firstname = trim($firstname);
if (!strlen($firstname)) {
$output->writeln('<error>Firstname cannot be empty</error>');
return Command::INVALID;
}

$question = new Question('Lastname: ', '');
$lastname = $questionHelper->ask($input, $output, $question);
$lastname = trim($lastname);
if (!strlen($lastname)) {
$output->writeln('<error>Lastname cannot be empty</error>');
return Command::INVALID;
}

$websites = [];
$websitesCollection = Mage::getResourceModel('core/website_collection');
foreach ($websitesCollection as $website) {
$websites[$website->getId()] = "{$website->getCode()} - {$website->getName()}";
}
$question = new ChoiceQuestion('Website: ', $websites);
$website = $questionHelper->ask($input, $output, $question);
$website = explode(" - ", $website)[0];
$website = Mage::getModel('core/website')->load($website, 'code');
if (!$website->getId()) {
$output->writeln('<error>Website not found</error>');
return Command::INVALID;
}

$customer = Mage::getModel('customer/customer')
->setEmail($email)
->setPassword($password)
->setFirstname($firstname)
->setLastname($lastname)
->setWebsiteId($website->getId())
->save();

if (!$customer->getId()) {
$output->writeln('<error>Unable to create customer</error>');
return Command::FAILURE;
}

$output->writeln("<info>Customer $email create successfully.</info>");
return Command::SUCCESS;
}
}
4 changes: 2 additions & 2 deletions maho
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ $application->add(new \MahoCLI\Commands\CacheEnable());
$application->add(new \MahoCLI\Commands\CacheDisable());
$application->add(new \MahoCLI\Commands\CacheFlush());

//$application->add(new \MahoCLI\Commands\CustomerCreate());
//$application->add(new \MahoCLI\Commands\CustomerChangePassword());
$application->add(new \MahoCLI\Commands\CustomerCreate());
$application->add(new \MahoCLI\Commands\CustomerChangePassword());
$application->add(new \MahoCLI\Commands\CustomerDelete());
$application->add(new \MahoCLI\Commands\CustomerList());

Expand Down

0 comments on commit 25c7f41

Please sign in to comment.