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

SubscriptionBuilder::withLastTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace ByTIC\Payments\Subscriptions;
4
5
use ByTIC\Payments\Models\BillingRecord\Traits\RecordTrait as BillingRecord;
6
use ByTIC\Payments\Models\Methods\PaymentMethod;
7
use ByTIC\Payments\Models\Methods\Traits\RecordTrait as PaymentMethodTrait;
8
use ByTIC\Payments\Models\Purchase\Traits\IsPurchasableModelTrait;
9
use ByTIC\Payments\Models\Transactions\Transaction;
10
use ByTIC\Payments\Models\Transactions\TransactionTrait;
11
use ByTIC\Payments\Subscriptions\Statuses\NotStarted;
12
use ByTIC\Payments\Utility\PaymentsModels;
13
use Nip\Utility\Date;
14
15
/**
16
 * Class SubscriptionBuilder
17
 * @package ByTIC\Payments\Utility
18
 */
19
class SubscriptionBuilder
20
{
21
    use Builder\ManageBillingCycle;
22
23
    /**
24
     * @var \ByTIC\Payments\Models\Subscriptions\Subscription
25
     */
26
    protected $subscription;
27
28
    /**
29
     * SubscriptionBuilder constructor.
30
     */
31
    protected function __construct()
32
    {
33
        $this->subscription = PaymentsModels::subscriptions()->getNew();
34
        $this->subscription->setPropertyValue('status', NotStarted::NAME);
0 ignored issues
show
Bug introduced by
It seems like setPropertyValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $this->subscription->/** @scrutinizer ignore-call */ 
35
                             setPropertyValue('status', NotStarted::NAME);
Loading history...
35
        $this->subscription->start_at = Date::now();
36
    }
37
38
    /**
39
     * @param IsPurchasableModelTrait $purchase
40
     */
41
    public static function fromPurchase($purchase): SubscriptionBuilder
42
    {
43
        $builder = new static();
44
        $builder->withPaymentMethod($purchase->getPaymentMethod());
45
        $builder->withCustomer($purchase->getPurchaseBillingRecord());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $purchase->getPurchaseBillingRecord() targeting ByTIC\Payments\Models\Pu...PurchaseBillingRecord() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
46
47
        $transaction = PaymentsModels::transactions()->findOrCreateForPurchase($purchase);
48
        $builder->withLastTransaction($transaction);
49
        return $builder;
50
    }
51
52
    /**
53
     * @param $status
54
     * @return SubscriptionBuilder
55
     */
56
    public function withStatus($status): SubscriptionBuilder
57
    {
58
        $this->subscription->setPropertyValue('status', $status);
59
        return $this;
60
    }
61
62
    /**
63
     * @param PaymentMethod|PaymentMethodTrait $method
64
     */
65
    public function withPaymentMethod($method): SubscriptionBuilder
66
    {
67
        $this->subscription->populateFromPaymentMethod($method);
68
        return $this;
69
    }
70
71
    /**
72
     * @param Transaction|TransactionTrait $transaction
73
     */
74
    public function withLastTransaction($transaction): SubscriptionBuilder
75
    {
76
        $this->subscription->populateFromLastTransaction($transaction);
77
        return $this;
78
    }
79
80
    /**
81
     * @param BillingRecord $customer
82
     */
83
    public function withCustomer($customer): SubscriptionBuilder
84
    {
85
        $this->subscription->populateFromCustomer($customer);
86
        return $this;
87
    }
88
89
    /**
90
     * @return \ByTIC\Payments\Models\Subscriptions\Subscription|\ByTIC\Payments\Models\Subscriptions\SubscriptionTrait
91
     */
92
    public function create()
93
    {
94
        $this->subscription->insert();
95
96
        $lastTransaction = $this->subscription->getLastTransaction();
0 ignored issues
show
Bug introduced by
The method getLastTransaction() does not exist on ByTIC\Payments\Models\Subscriptions\Subscription. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        /** @scrutinizer ignore-call */ 
97
        $lastTransaction = $this->subscription->getLastTransaction();
Loading history...
97
        $lastTransaction->populateFromSubscription($this->subscription);
0 ignored issues
show
Bug introduced by
The method populateFromSubscription() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        $lastTransaction->/** @scrutinizer ignore-call */ 
98
                          populateFromSubscription($this->subscription);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
        $lastTransaction->update();
0 ignored issues
show
Bug introduced by
The method update() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        $lastTransaction->/** @scrutinizer ignore-call */ 
99
                          update();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
100
        return $this->subscription;
101
    }
102
}