the browser-facing portion of osu!
at master 1.9 kB view raw
1<?php 2 3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4// See the LICENCE file in the repository root for full licence text. 5 6namespace App\Models; 7 8use Carbon\Carbon; 9use DB; 10use Log; 11 12/** 13 * FIXME: should validate donation is a positive value on save. 14 * 15 * @property int $amount 16 * @property bool $cancel 17 * @property int $length 18 * @property int $target_user_id 19 * @property \Carbon\Carbon $timestamp 20 * @property string $transaction_id 21 * @property int $user_id 22 */ 23class UserDonation extends Model 24{ 25 public $incrementing = false; 26 public $timestamps = false; 27 28 protected $casts = [ 29 'cancel' => 'boolean', 30 'timestamp' => 'datetime', 31 ]; 32 protected $primaryKey = ':composite'; 33 protected $primaryKeys = ['user_id', 'transaction_id']; 34 protected $table = 'osu_user_donations'; 35 36 public static function totalLength($userId) 37 { 38 $lengths = static::where('target_user_id', $userId) 39 ->select('cancel', DB::raw('SUM(length) as length')) 40 ->groupBy('cancel') 41 ->get(); 42 43 $totalLength = 0; 44 45 foreach ($lengths as $length) { 46 if ($length->cancel !== true) { 47 $totalLength += $length->length; 48 } else { 49 $totalLength -= $length->length; 50 } 51 } 52 53 return $totalLength; 54 } 55 56 public function cancel($cancelledTransactionId) 57 { 58 if ($this->cancel) { 59 Log::warning("UserDonation({$this->getKey()}) Calling cancel on a cancelled donation"); 60 61 return; 62 } 63 64 $donation = $this->replicate(); 65 $donation->transaction_id = $cancelledTransactionId; 66 $donation->amount = -$donation->amount; 67 $donation->cancel = true; 68 $donation->timestamp = Carbon::now(); 69 70 Log::debug($donation); 71 72 $donation->saveOrExplode(); 73 } 74}