public function recalculate() { if (!$this->user instanceof User) { throw new InvalidDataException("User not set"); } $runningTotal = 0; $positivePayments = $this->paymentRepository->getUserPaymentsByReason($this->userId, 'balance'); $negativePayments = $this->paymentRepository->getUserPaymentsBySource($this->userId, 'balance'); foreach ($positivePayments as $payment) { $runningTotal = $runningTotal + $payment->amount * 100; } foreach ($negativePayments as $payment) { $runningTotal = $runningTotal - $payment->amount * 100; } $this->user->cash_balance = $runningTotal; $this->user->save(); }