magento2-docker/app/code/IpSupply/CustomerErp/Helper/CustomerHelper.php

75 lines
2.4 KiB
PHP
Executable File

<?php
namespace IpSupply\CustomerErp\Helper;
class CustomerHelper
{
protected $objectManager;
protected $cache;
public function __construct()
{
$this->objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->cache = $this->objectManager->create('\Magento\Framework\App\CacheInterface');
}
public function createCustomer($email, $firstName, $lastName, $password)
{
try {
$customer = $this->objectManager->get('\Magento\Customer\Api\Data\CustomerInterfaceFactory')->create();
$customer->setWebsiteId(1);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$hashedPassword = $this->objectManager->get('\Magento\Framework\Encryption\EncryptorInterface')->getHash($password, true);
$this->objectManager->get('\Magento\Customer\Api\CustomerRepositoryInterface')->save($customer, $hashedPassword);
$customer = $this->objectManager->get('\Magento\Customer\Model\CustomerFactory')->create();
$customer->setWebsiteId(1)->loadByEmail($email);
$customer->setPasswordHash($this->createPasswordHash($password));
$customer->setConfirmation(null);
$customer->save();
return $customer;
} catch (\Exception $e) {
return null;
}
return null;
}
public function getCustomerByEmail($email)
{
try {
$customer = $this->objectManager->create('Magento\Customer\Model\Customer');
$customer->setWebsiteId(1);
$customer->loadByEmail($email);
if ($customer->getId()){
return $customer;
}
} catch (\Exception $e) {
return null;
}
return null;
}
public function deleteCustomerByEmail($email)
{
try {
$this->objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true);
$customer = $this->objectManager->create('Magento\Customer\Model\Customer');
$customer->setWebsiteId(1);
$customer->loadByEmail($email);
if ($customer->getId()){
$customer->delete();
}
} catch (\Exception $e) {
}
}
public function createPasswordHash($password)
{
return password_hash($password, PASSWORD_DEFAULT);
}
}