Files
2026-09-15 10:26:34 +08:00

105 lines
4.6 KiB
PHP

<?php
namespace Stemcell\Customer\Model\ResourceModel;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\Data\CustomerSearchResultsInterfaceFactory;
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Customer\Model\Customer\NotificationStorage;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\CustomerRegistry;
use Magento\Customer\Model\Data\CustomerSecureFactory;
use Magento\Customer\Model\Delegation\Storage as DelegatedStorage;
use Magento\Customer\Model\ResourceModel;
use Magento\Customer\Model\ResourceModel\AddressRepository;
use Magento\Customer\Model\ResourceModel\Customer;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Framework\Api\ImageProcessorInterface;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
use Psr\Log\LoggerInterface;
class CustomerRepository extends ResourceModel\CustomerRepository
{
const MOBILE_PATTERN = '/^(1(([3456789][0-9])|(47)|[8][01236789]))\d{8}$/';
protected CollectionFactory $customerCollectionFactory;
public function __construct(
CustomerFactory $customerFactory,
CustomerSecureFactory $customerSecureFactory,
CustomerRegistry $customerRegistry,
AddressRepository $addressRepository,
Customer $customerResourceModel,
CustomerMetadataInterface $customerMetadata,
CustomerSearchResultsInterfaceFactory $searchResultsFactory,
ManagerInterface $eventManager,
StoreManagerInterface $storeManager,
ExtensibleDataObjectConverter $extensibleDataObjectConverter,
DataObjectHelper $dataObjectHelper,
ImageProcessorInterface $imageProcessor,
JoinProcessorInterface $extensionAttributesJoinProcessor,
CollectionProcessorInterface $collectionProcessor,
NotificationStorage $notificationStorage,
CollectionFactory $customerCollectionFactory,
DelegatedStorage $delegatedStorage = null,
?GroupRepositoryInterface $groupRepository = null
) {
$this->customerCollectionFactory = $customerCollectionFactory;
parent::__construct($customerFactory, $customerSecureFactory, $customerRegistry, $addressRepository, $customerResourceModel, $customerMetadata, $searchResultsFactory, $eventManager, $storeManager, $extensibleDataObjectConverter, $dataObjectHelper, $imageProcessor, $extensionAttributesJoinProcessor, $collectionProcessor, $notificationStorage, $delegatedStorage, $groupRepository);
}
/**
* @param String $mobile
* @param null $websiteId
* @return \Magento\Customer\Model\Customer
* @throws NoSuchEntityException
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function getCustomerByMobile(String $mobile, $websiteId = null) {
$websiteId = $websiteId?: $this->storeManager->getDefaultStoreView()->getWebsiteId();
/* @var $customer \Magento\Customer\Model\Customer */
$customer = $this->customerCollectionFactory->create()
->addAttributeToSelect('*')
->addAttributeToFilter('mobile', $mobile)
->addAttributeToFilter(CustomerInterface::WEBSITE_ID, $websiteId)
->getFirstItem();
if (!$customer->getId()) {
throw new NoSuchEntityException(
__(
'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value',
[
'fieldName' => 'mobile',
'fieldValue' => $mobile,
'field2Name' => 'websiteId',
'field2Value' => $websiteId
]
)
);
}
return $customer;
}
/**
* @param string $email
* @param null $websiteId
* @return \Magento\Customer\Api\Data\CustomerInterface
* @throws NoSuchEntityException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function get($email, $websiteId = null)
{
if (preg_match(self::MOBILE_PATTERN, $email)) {
return $this->getCustomerByMobile($email, $websiteId)->getDataModel();
}
return parent::get($email, $websiteId);
}
}