diff --git a/src/Benzina/InvestRewardsPump.php b/src/Benzina/InvestRewardsPump.php new file mode 100644 index 0000000..1cd96b0 --- /dev/null +++ b/src/Benzina/InvestRewardsPump.php @@ -0,0 +1,153 @@ + */ + private array $userCache = []; + + /** @var array */ + private array $chargeCache = []; + + /** @var array */ + private array $rewardCache = []; + + public function __construct( + private RewardRepository $rewardRepository, + private UserRepository $userRepository, + private ChargeRepository $chargeRepository, + private MoneyService $moneyService, + ) {} + + public function supports(mixed $sample): bool + { + if ($this->hasAllKeys($sample, ['invest', 'reward', 'fulfilled'])) { + return true; + } + + return false; + } + + public function pump(mixed $record, array $context): void + { + $reward = $this->getReward($record); + if ($reward === null) { + return; + } + + $invest = $this->getInvest($record, $context); + if ($invest === null) { + return; + } + + $charge = $this->getCharge($invest); + if ($charge === null) { + return; + } + + $user = $this->getUser($invest); + if ($user === null) { + return; + } + + if ($charge->getStatus() !== ChargeStatus::Charged) { + return; + } + + $claim = new RewardClaim(); + $claim->setOwner($user); + $claim->setCharge($charge); + + $reward->addClaim($claim); + $claim->setReward($reward); + + $this->persist($claim, $context); + } + + private function getInvest(array $record, array $context): ?array + { + $query = $this->getDbConnection($context)->prepare( + 'SELECT * FROM `invest` i WHERE i.id = :invest' + ); + + $query->execute(['invest' => $record['invest']]); + + $result = $query->fetch(\PDO::FETCH_ASSOC); + + if (!$result) { + return null; + } + + return $result; + } + + private function getReward(array $record): ?Reward + { + $id = $record['reward']; + + if (isset($this->rewardCache[$id])) { + return $this->rewardRepository->find($this->rewardCache[$id]); + } + + $reward = $this->rewardRepository->findOneBy(['migratedId' => $id]); + if (!$reward) { + return null; + } + + $this->rewardCache[$id] = $reward->getId(); + + return $reward; + } + + private function getCharge(array $invest): ?Charge + { + $id = $invest['id']; + + if (isset($this->chargeCache[$id])) { + return $this->chargeRepository->find($this->chargeCache[$id]); + } + + $charge = $this->chargeRepository->findOneBy(['migratedId' => $id]); + if (!$charge) { + return null; + } + + $this->chargeCache[$id] = $charge->getId(); + + return $charge; + } + + private function getUser(array $invest): ?User + { + $id = $invest['user']; + + if (isset($this->userCache[$id])) { + return $this->userRepository->find($this->userCache[$id]); + } + + $user = $this->userRepository->findOneBy(['migratedId' => $id]); + + $this->userCache[$id] = $user->getId(); + + return $user; + } +}