Passed
Push — master ( 96d33b...43186b )
by Gabriel
13:27
created

ManageBillingCycle   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 53
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A monthly() 0 3 1
A yearly() 0 4 1
A daily() 0 3 1
A weekly() 0 3 1
A billEvery() 0 8 2
1
<?php
2
3
namespace ByTIC\Payments\Subscriptions\Builder;
4
5
use ByTIC\Payments\Subscriptions\Billing\BillingPeriod;
6
use Exception;
7
8
/**
9
 * Trait ManageBillingCycle
10
 * @package ByTIC\Payments\Subscriptions\Builder
11
 */
12
trait ManageBillingCycle
13
{
14
15
    /**
16
     * @param int $every
17
     * @return self
18
     */
19
    public function daily($every = 1)
20
    {
21
        return $this->billEvery($every, BillingPeriod::DAILY);
22
    }
23
24
    /**
25
     * @param int $every
26
     * @return self
27
     */
28
    public function weekly($every = 1)
29
    {
30
        return $this->billEvery($every, BillingPeriod::WEEKLY);
31
    }
32
33
    /**
34
     * @param int $every
35
     * @return self
36
     */
37
    public function monthly($every = 1)
38
    {
39
        return $this->billEvery($every, BillingPeriod::MONTHLY);
40
    }
41
42
    /**
43
     * @param int $every
44
     * @return self
45
     */
46
    public function yearly($every = 1)
47
    {
48
        /** @noinspection PhpUnhandledExceptionInspection */
49
        return $this->billEvery($every, BillingPeriod::YEARLY);
50
    }
51
52
    /**
53
     * @param int $interval
54
     * @param string $period
55
     * @return $this
56
     */
57
    public function billEvery(int $interval, string $period)
58
    {
59
        if (!in_array($period, BillingPeriod::PERIOD)) {
60
            throw new Exception("Invalid period [{$interval}] in " . get_class($this));
61
        }
62
        $this->subscription->billing_interval = $interval;
63
        $this->subscription->billing_period = $period;
64
        return $this;
65
    }
66
}