-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from tipoff/pbdreen/feature/has-statuses
Add HasStatuses trait and supporting code
- Loading branch information
Showing
14 changed files
with
544 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tipoff\Statuses\Database\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Tipoff\Authorization\Models\User; | ||
use Tipoff\Statuses\Models\Status; | ||
use Tipoff\Statuses\Models\StatusRecord; | ||
|
||
class StatusRecordFactory extends Factory | ||
{ | ||
protected $model = StatusRecord::class; | ||
|
||
public function definition() | ||
{ | ||
$statusable = User::factory()->create(); | ||
$status = randomOrCreate(Status::class); | ||
|
||
return [ | ||
'status_id' => $status, | ||
'type' => $status->type, | ||
'statusable_type' => get_class($statusable), | ||
'statusable_id' => $statusable->id, | ||
'creator_id' => randomOrCreate(app('user')), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tipoff\Statuses\Exceptions; | ||
|
||
interface StatusException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tipoff\Statuses\Exceptions; | ||
|
||
use Throwable; | ||
|
||
class UnknownStatusException extends \InvalidArgumentException implements StatusException | ||
{ | ||
public function __construct(string $type, string $name, $code = 0, Throwable $previous = null) | ||
{ | ||
parent::__construct("Unknown status value '{$name}' for status type {$type}", $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tipoff\Statuses\Models; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Tipoff\Authorization\Models\User; | ||
use Tipoff\Support\Models\BaseModel; | ||
use Tipoff\Support\Traits\HasCreator; | ||
use Tipoff\Support\Traits\HasPackageFactory; | ||
|
||
/** | ||
* @property int id | ||
* @property Status status | ||
* @property Model statusable | ||
* @property User creator | ||
* @property Carbon created_at | ||
* // Raw Relations | ||
* @property int|null creator_id | ||
*/ | ||
class StatusRecord extends BaseModel | ||
{ | ||
use HasPackageFactory; | ||
use HasCreator; | ||
|
||
const UPDATED_AT = null; | ||
|
||
protected $casts = [ | ||
'id' => 'integer', | ||
'creator_id' => 'integer', | ||
]; | ||
|
||
public function status() | ||
{ | ||
return $this->belongsTo(Status::class); | ||
} | ||
|
||
public function statusable() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tipoff\Statuses\Traits; | ||
|
||
use Illuminate\Support\Collection; | ||
use Tipoff\Statuses\Exceptions\UnknownStatusException; | ||
use Tipoff\Statuses\Models\Status; | ||
use Tipoff\Statuses\Models\StatusRecord; | ||
|
||
/** | ||
* @property Collection statusables | ||
*/ | ||
trait HasStatuses | ||
{ | ||
// When true, new statuses will be added automatically on first use | ||
// When false, exception occurs if unknown status value is used | ||
protected bool $dynamicStatusCreation = false; | ||
|
||
public function getStatusHistory(?string $type = null): Collection | ||
{ | ||
return $this->statusRecords() | ||
->where('type', '=', $type ?? get_class($this)) | ||
->orderBy('created_at', 'desc') | ||
->orderBy('id', 'desc') | ||
->get(); | ||
} | ||
|
||
public function getStatus(?string $type = null): ?Status | ||
{ | ||
$statusRecord = $this->getStatusHistory($type)->first(); | ||
|
||
return $statusRecord ? $statusRecord->status : null; | ||
} | ||
|
||
public function setStatus(string $name, ?string $type = null): Status | ||
{ | ||
$type = $type ?? get_class($this); | ||
|
||
$status = $this->getStatusByName($name, $type); | ||
|
||
// Check if desired status already set | ||
if ($currentStatus = $this->getStatus($type)) { | ||
if ($currentStatus->id === $status->id) { | ||
return $currentStatus; | ||
} | ||
} | ||
|
||
// Set status always creates new record, leaving full history behind | ||
$statusable = new StatusRecord(); | ||
$statusable->type = $type; | ||
$statusable->statusable()->associate($this); | ||
|
||
$statusable->status()->associate($status)->save(); | ||
$this->load('statusRecords'); | ||
|
||
return $statusable->status; | ||
} | ||
|
||
public function statusRecords() | ||
{ | ||
return $this->morphMany(StatusRecord::class, 'statusable'); | ||
} | ||
|
||
private function getStatusByName(string $name, string $type): Status | ||
{ | ||
if ($this->dynamicStatusCreation) { | ||
return Status::createStatus($type, $name); | ||
} | ||
|
||
if ($status = Status::findStatus($type, $name)) { | ||
return $status; | ||
} | ||
|
||
throw new UnknownStatusException($type, $name); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.