1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sagitarius29\LaravelSubscriptions\Entities; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\PlanIntervalContract; |
7
|
|
|
use Sagitarius29\LaravelSubscriptions\Exceptions\IntervalErrorException; |
8
|
|
|
|
9
|
|
|
class PlanInterval extends Model implements PlanIntervalContract |
10
|
|
|
{ |
11
|
|
|
protected $table = 'plan_intervals'; |
12
|
|
|
protected $fillable = [ |
13
|
|
|
'price', 'type', 'unit', |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
public static function make($type, int $unit, float $price): PlanIntervalContract |
17
|
|
|
{ |
18
|
|
|
self::checkIfIntervalExists($type); |
19
|
|
|
|
20
|
|
|
$attributes = [ |
21
|
|
|
'price' => $price, |
22
|
|
|
'type' => $type, |
23
|
|
|
'unit' => $unit, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
return new self($attributes); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected static function checkIfIntervalExists(string $interval) |
30
|
|
|
{ |
31
|
|
|
$intervals = [ |
32
|
|
|
self::DAY, self::MONTH, self::YEAR, |
33
|
|
|
]; |
34
|
|
|
if (! in_array($interval, $intervals)) { |
35
|
|
|
throw new IntervalErrorException( |
36
|
|
|
'\''.$interval.'\' is not correct. Available intervals are: '.implode(', ', $intervals) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function makeInfinite(float $price): PlanIntervalContract |
42
|
|
|
{ |
43
|
|
|
return new self([ |
44
|
|
|
'price' => $price, |
45
|
|
|
]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function plan() |
49
|
|
|
{ |
50
|
|
|
return $this->belongsTo(config('subscriptions.entities.plan')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getUnit(): int |
54
|
|
|
{ |
55
|
|
|
return $this->unit; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function isInfinite(): bool |
59
|
|
|
{ |
60
|
|
|
return $this->getType() == null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getType(): ?string |
64
|
|
|
{ |
65
|
|
|
return $this->type; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function isFree(): bool |
69
|
|
|
{ |
70
|
|
|
return $this->getPrice() == 0; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getPrice(): float |
74
|
|
|
{ |
75
|
|
|
return $this->price; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function isNotFree(): bool |
79
|
|
|
{ |
80
|
|
|
return $this->getPrice() != 0; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|