/**
  * Handle the event.
  *
  * @param  TransactionWasCreated  $event
  * @return void
  */
 public function handle($event)
 {
     switch (class_basename($event)) {
         case "TransactionWasCreated":
             $transaction = $event->transaction;
             // Put an amount into savings if it is an income transaction
             if ($transaction->type === Transaction::TYPE_INCOME) {
                 $savings = Savings::forCurrentUser()->first();
                 $savings->increase($this->savingsRepository->calculateAfterIncomeAdded($transaction));
             }
             break;
         case "TransactionWasUpdated":
             $oldTotal = $event->oldTransaction->total;
             if (!isset($event->newTransaction['total']) && !isset($event->newTransaction['type'])) {
                 return;
             }
             if (isset($event->newTransaction['total'])) {
                 $newTotal = $event->newTransaction['total'];
             }
             $savings = Savings::forCurrentUser()->first();
             if (isset($event->newTransaction['type']) && $event->oldTransaction->type !== $event->newTransaction['type']) {
                 //The transaction type has changed
                 if ($event->newTransaction['type'] === 'income') {
                     //Increase the savings
                     if (isset($event->newTransaction['total'])) {
                         $savings->increase($event->newTransaction['total'] / 10);
                     } else {
                         $savings->increase($event->oldTransaction->total / 10);
                     }
                 } else {
                     if ($event->newTransaction['type'] === 'expense') {
                         //Decrease the savings
                         $savings->decrease($event->oldTransaction->total * -1 / 10);
                     }
                 }
             } else {
                 //The transaction type is the same
                 // If it is an income transaction, and if the total has decreased,
                 // remove a percentage from savings
                 if ($event->oldTransaction->type === 'income' && $newTotal < $oldTotal) {
                     $savings->decrease($this->savingsRepository->calculateAfterDecrease($oldTotal, $newTotal));
                 }
                 // If it is an income transaction, and if the total has increased,
                 // add a percentage to savings
                 if ($event->oldTransaction->type === 'income' && $newTotal > $oldTotal) {
                     $savings->increase($this->savingsRepository->calculateAfterIncrease($oldTotal, $newTotal));
                 }
             }
             break;
         default:
     }
 }
 /**
  * Delete a transaction, only if it belongs to the user
  * @param Transaction $transaction
  * @return Response
  * @throws \Exception
  */
 public function destroy(Transaction $transaction)
 {
     $transaction->delete();
     //Reverse the automatic insertion into savings if it is an income expense
     if ($transaction->type === 'income') {
         $savings = Savings::forCurrentUser()->first();
         $savings->decrease($this->savingsRepository->calculateAmountToSubtract($transaction));
     }
     return $this->responseNoContent();
 }