Skip to content

Commit

Permalink
Added PriceStatus enum
Browse files Browse the repository at this point in the history
  • Loading branch information
voidgraphics committed Mar 6, 2023
1 parent d512fce commit 79d2fe7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/Enums/PriceStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Whitecube\LaravelPrices\Enums;

enum PriceStatus
{
case SCHEDULED;
case CURRENT;
case EXPIRED;
}
20 changes: 10 additions & 10 deletions src/Models/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
use Illuminate\Database\Eloquent\Model;
use Whitecube\Price\Price as PriceObject;
use Whitecube\LaravelPrices\Concerns\HasUuid;
use Whitecube\LaravelPrices\Exceptions\PriceValueNotDefinedException;
use Whitecube\LaravelPrices\Enums\PriceStatus;

class Price extends Model
{
use HasUuid;

public $timestamps = ['activated_at'];

protected $guarded = [];

protected $casts = [
'amount' => 'integer'
'amount' => 'integer',
'activated_at' => 'datetime',
];

public function __construct(
Expand Down Expand Up @@ -80,17 +79,18 @@ public function toObject(): PriceObject
return PriceObject::ofMinor($this->amount, $this->currency);
}

public function getStatusAttribute()
public function getStatusAttribute(): PriceStatus
{
if($this->activated_at > now()) {
return 'programmed';
return PriceStatus::SCHEDULED;
}

$current = $this->priceable->price()->first();
if($current->id == $this->id) {
return 'current';
$current = $this->priceable->price()->current()->first();

if ($current->id == $this->id) {
return PriceStatus::CURRENT;
}

return 'passed';
return PriceStatus::EXPIRED;
}
}

0 comments on commit 79d2fe7

Please sign in to comment.