| Current Path : /home/rtorresani/www/app/code/Amasty/Rewards/Setup/Patch/Data/ |
| Current File : //home/rtorresani/www/app/code/Amasty/Rewards/Setup/Patch/Data/MigrateCustomerBalance.php |
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) 2023 Amasty (https://www.amasty.com)
* @package Reward Points Base for Magento 2
*/
namespace Amasty\Rewards\Setup\Patch\Data;
use Amasty\Rewards\Model\CustomerBalanceFactory;
use Amasty\Rewards\Model\Repository\CustomerBalanceRepository;
use Amasty\Rewards\Model\Repository\RewardsRepository;
use Amasty\Rewards\Model\ResourceModel\Rewards\CollectionFactory;
use Magento\Framework\DB\Select;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class MigrateCustomerBalance implements DataPatchInterface
{
/**
* @var CustomerBalanceFactory
*/
private $customerBalanceFactory;
/**
* @var CustomerBalanceRepository
*/
private $customerBalanceRepository;
/**
* @var RewardsRepository
*/
private $rewardsRepository;
/**
* @var CollectionFactory
*/
private $rewardsCollectionFactory;
public function __construct(
CustomerBalanceRepository $customerBalanceRepository,
CustomerBalanceFactory $customerBalanceFactory,
RewardsRepository $rewardsRepository,
CollectionFactory $rewardsCollectionFactory
) {
$this->customerBalanceRepository = $customerBalanceRepository;
$this->customerBalanceFactory = $customerBalanceFactory;
$this->rewardsRepository = $rewardsRepository;
$this->rewardsCollectionFactory = $rewardsCollectionFactory;
}
public static function getDependencies(): array
{
return [];
}
public function getAliases(): array
{
return [];
}
public function apply(): self
{
$customerIds = $this->getCustomerIds();
foreach ($customerIds as $customerId) {
$customerBalanceEntity = $this->customerBalanceFactory->create();
$customerBalanceEntity->setCustomerId((int)$customerId);
$customerBalanceEntity->setBalance(
(float)$this->rewardsRepository->getCustomerRewardBalance((int)$customerId)
);
$this->customerBalanceRepository->save($customerBalanceEntity);
}
return $this;
}
/**
* @return string[]
*/
private function getCustomerIds(): array
{
$rewardsCollection = $this->rewardsCollectionFactory->create();
$select = $rewardsCollection->getSelect()
->reset(Select::COLUMNS)
->columns('customer_id')
->group('customer_id');
return $rewardsCollection->getConnection()->fetchCol($select);
}
}