Completed
Push — develop ( 6deca1...bf40a6 )
by Adolfo
01:08
created

PlanPrice::checkPriceInterval()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
4
namespace Sagitarius29\LaravelSubscriptions\Entities;
5
6
7
use Illuminate\Database\Eloquent\Model;
8
use Sagitarius29\LaravelSubscriptions\Contracts\PlanPriceContract;
9
use Sagitarius29\LaravelSubscriptions\Exceptions\PriceErrorException;
10
11
class PlanPrice extends Model implements PlanPriceContract
12
{
13
    protected $table = 'plan_prices';
14
15
    protected $fillable = [
16
        'amount', 'interval', 'interval_unit'
17
    ];
18
19
    public static $DAY      = 'day';
20
    public static $MONTH    = 'month';
21
    public static $YEAR     = 'year';
22
23
    public function plan()
24
    {
25
        return $this->belongsTo(Plan::class);
26
    }
27
28
    public function getAmount(): int
29
    {
30
        return $this->amount;
31
    }
32
33
    public static function make($interval, int $intervalUnit, float $amount): PlanPriceContract
34
    {
35
        self::checkPriceInterval($interval);
36
37
        $attributes = [
38
            'amount'        => $amount,
39
            'interval'      => $interval,
40
            'interval_unit' => $intervalUnit
41
        ];
42
43
        return new self($attributes);
44
    }
45
46
    public static function makeWithoutInterval(float $amount): PlanPriceContract
47
    {
48
        return new self([
49
            'amount'    => $amount
50
        ]);
51
    }
52
53
    public function getInterval(): string
54
    {
55
        return $this->interval;
56
    }
57
58
    public function getIntervalUnit(): int
59
    {
60
        return $this->interval_unit;
61
    }
62
63
    protected static function checkPriceInterval(string $interval)
64
    {
65
        $intervals = [
66
            self::$DAY, self::$MONTH, self::$YEAR
67
        ];
68
        if(!in_array($interval, $intervals)) {
69
            throw new PriceErrorException(
70
                'The interval \'' . $interval . '\' is not correct. Available intervals are: ' . implode(', ', $intervals)
71
            );
72
        }
73
    }
74
}
75