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