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