Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (38 sloc) 1.05 KB
<?php
namespace App;
use App\Events\AccountCreated;
use App\Events\MoneyAdded;
use App\Events\MoneySubtracted;
use Illuminate\Database\Eloquent\Model;
use Ramsey\Uuid\Uuid;
class Account extends Model
{
public $guarded = [];
public static function createWithAttributes(array $attributes): Account
{
/*
* Let's generate a uuid.
*/
$attributes['uuid'] = (string) Uuid::uuid4();
/*
* The account will be created inside this event using the generated uuid.
*/
event(new AccountCreated($attributes));
/*
* The uuid will be used the retrieve the created account.
*/
return static::uuid($attributes['uuid']);
}
public function addMoney(int $amount)
{
event(new MoneyAdded($this->uuid, $amount));
}
public function subtractMoney(int $amount)
{
event(new MoneySubtracted($this->uuid, $amount));
}
public static function uuid(string $uuid): ?Account
{
return static::where('uuid', $uuid)->first();
}
}
You can’t perform that action at this time.