2626use Magento \Framework \Api \SearchCriteria \CollectionProcessorInterface ;
2727use Magento \Framework \Api \SearchCriteriaInterface ;
2828use Magento \Framework \App \ObjectManager ;
29+ use Magento \Framework \EntityManager \HydratorInterface ;
2930use Magento \Framework \Event \ManagerInterface ;
3031use Magento \Store \Model \StoreManagerInterface ;
3132
@@ -119,6 +120,11 @@ class CustomerRepository implements CustomerRepositoryInterface
119120 */
120121 private $ delegatedStorage ;
121122
123+ /**
124+ * @var HydratorInterface
125+ */
126+ private $ hydrator ;
127+
122128 /**
123129 * @param CustomerFactory $customerFactory
124130 * @param CustomerSecureFactory $customerSecureFactory
@@ -136,6 +142,7 @@ class CustomerRepository implements CustomerRepositoryInterface
136142 * @param CollectionProcessorInterface $collectionProcessor
137143 * @param NotificationStorage $notificationStorage
138144 * @param DelegatedStorage|null $delegatedStorage
145+ * @param HydratorInterface|null $hydrator
139146 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
140147 */
141148 public function __construct (
@@ -154,7 +161,8 @@ public function __construct(
154161 JoinProcessorInterface $ extensionAttributesJoinProcessor ,
155162 CollectionProcessorInterface $ collectionProcessor ,
156163 NotificationStorage $ notificationStorage ,
157- DelegatedStorage $ delegatedStorage = null
164+ DelegatedStorage $ delegatedStorage = null ,
165+ ?HydratorInterface $ hydrator = null
158166 ) {
159167 $ this ->customerFactory = $ customerFactory ;
160168 $ this ->customerSecureFactory = $ customerSecureFactory ;
@@ -172,6 +180,7 @@ public function __construct(
172180 $ this ->collectionProcessor = $ collectionProcessor ;
173181 $ this ->notificationStorage = $ notificationStorage ;
174182 $ this ->delegatedStorage = $ delegatedStorage ?? ObjectManager::getInstance ()->get (DelegatedStorage::class);
183+ $ this ->hydrator = $ hydrator ?: ObjectManager::getInstance ()->get (HydratorInterface::class);
175184 }
176185
177186 /**
@@ -185,6 +194,7 @@ public function __construct(
185194 * @throws \Magento\Framework\Exception\LocalizedException
186195 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
187196 * @SuppressWarnings(PHPMD.NPathComplexity)
197+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
188198 */
189199 public function save (CustomerInterface $ customer , $ passwordHash = null )
190200 {
@@ -193,10 +203,11 @@ public function save(CustomerInterface $customer, $passwordHash = null)
193203 $ prevCustomerData = $ prevCustomerDataArr = null ;
194204 if ($ customer ->getId ()) {
195205 $ prevCustomerData = $ this ->getById ($ customer ->getId ());
196- $ prevCustomerDataArr = $ prevCustomerData ->__toArray ();
206+ $ prevCustomerDataArr = $ this ->hydrator ->extract ($ prevCustomerData );
207+ $ customer = $ this ->hydrator ->hydrate ($ prevCustomerData , $ customer ->__toArray ());
197208 }
198209 /** @var $customer \Magento\Customer\Model\Data\Customer */
199- $ customerArr = $ customer -> __toArray ( );
210+ $ customerArr = $ this -> hydrator -> extract ( $ customer );
200211 $ customer = $ this ->imageProcessor ->save (
201212 $ customer ,
202213 CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER ,
0 commit comments