|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Customer\Model\ResourceModel; |
| 8 | + |
| 9 | +/** |
| 10 | + * Customer repository. |
| 11 | + */ |
| 12 | +class CustomerAuthenticationRepository |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var \Magento\Customer\Model\CustomerRegistry |
| 16 | + */ |
| 17 | + protected $customerRegistry; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var \Magento\Customer\Model\ResourceModel\Customer |
| 21 | + */ |
| 22 | + protected $customerResourceModel; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var \Magento\Framework\Event\ManagerInterface |
| 26 | + */ |
| 27 | + protected $eventManager; |
| 28 | + |
| 29 | + /** |
| 30 | + * @param \Magento\Customer\Model\CustomerRegistry $customerRegistry |
| 31 | + * @param \Magento\Customer\Model\ResourceModel\Customer $customerResourceModel |
| 32 | + * @param \Magento\Framework\Event\ManagerInterface $eventManager |
| 33 | + */ |
| 34 | + public function __construct( |
| 35 | + \Magento\Customer\Model\CustomerRegistry $customerRegistry, |
| 36 | + \Magento\Customer\Model\ResourceModel\Customer $customerResourceModel, |
| 37 | + \Magento\Framework\Event\ManagerInterface $eventManager |
| 38 | + ) { |
| 39 | + $this->customerRegistry = $customerRegistry; |
| 40 | + $this->customerResourceModel = $customerResourceModel; |
| 41 | + $this->eventManager = $eventManager; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Create or update a customer. |
| 46 | + * |
| 47 | + * @param \Magento\Customer\Api\Data\CustomerInterface $customer |
| 48 | + * @return \Magento\Customer\Api\Data\CustomerInterface |
| 49 | + */ |
| 50 | + public function save(\Magento\Customer\Api\Data\CustomerInterface $customer) |
| 51 | + { |
| 52 | + $customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId()); |
| 53 | + |
| 54 | + $this->customerResourceModel->getConnection()->update( |
| 55 | + $this->customerResourceModel->getTable('customer_entity'), |
| 56 | + array( |
| 57 | + 'failures_num' => $customerSecure->getData('failures_num'), |
| 58 | + 'first_failure' => $customerSecure->getData('first_failure'), |
| 59 | + 'lock_expires' => $customerSecure->getData('lock_expires'), |
| 60 | + ), |
| 61 | + $this->customerResourceModel->getConnection()->quoteInto('entity_id = ?', $customer->getId()) |
| 62 | + ); |
| 63 | + |
| 64 | + $savedCustomer = $this->get($customer->getEmail(), $customer->getWebsiteId()); |
| 65 | + $this->eventManager->dispatch( |
| 66 | + 'customer_save_after_data_object', |
| 67 | + ['customer_data_object' => $savedCustomer, 'orig_customer_data_object' => $customer] |
| 68 | + ); |
| 69 | + return $savedCustomer; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Retrieve customer. |
| 74 | + * |
| 75 | + * @param string $email |
| 76 | + * @param int|null $websiteId |
| 77 | + * @return \Magento\Customer\Api\Data\CustomerInterface |
| 78 | + * @throws \Magento\Framework\Exception\NoSuchEntityException If customer with the specified email does not exist. |
| 79 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 80 | + */ |
| 81 | + public function get($email, $websiteId = null) |
| 82 | + { |
| 83 | + $customerModel = $this->customerRegistry->retrieveByEmail($email, $websiteId); |
| 84 | + return $customerModel->getDataModel(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get customer by customer ID. |
| 89 | + * |
| 90 | + * @param int $customerId |
| 91 | + * @return \Magento\Customer\Api\Data\CustomerInterface |
| 92 | + * @throws \Magento\Framework\Exception\NoSuchEntityException If customer with the specified ID does not exist. |
| 93 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 94 | + */ |
| 95 | + public function getById($customerId) |
| 96 | + { |
| 97 | + $customer = $this->customerRegistry->retrieve($customerId); |
| 98 | + return $customer->getDataModel(); |
| 99 | + } |
| 100 | +} |
0 commit comments