userMerchantStore = $userMerchantStore; $this->userMerchantBuilder = $userMerchantBuilder; } // saveCreditHistory public function save(CreditHistoryInterface $creditHistory) { if ($creditHistory) { $userMerchant = $creditHistory->getUserMerchant(); $isCreditActive = $this->userMerchantStore->isCreditActive($userMerchant); if ($isCreditActive) { if ($creditHistory->getType() == CreditHistoryModel::TYPE_CREDIT) { $userMerchantAmount = $userMerchant->getCredit() + $creditHistory->getAmountInherited(); $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount); return true; } elseif ($creditHistory->getType() == CreditHistoryModel::TYPE_DEBIT) { if ($this->userMerchantStore->isCreditSufficientToPay($userMerchant, $creditHistory->getAmountInherited())) { $userMerchantAmount = $userMerchant->getCredit() - $creditHistory->getAmountInherited(); $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount); return true; } else { return false; } } } } return false; } }