Issues (193)

src/Subscriptions/SubscriptionBuilder.php (3 issues)

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\ChargeMethods\Internal;
12
use ByTIC\Payments\Subscriptions\Statuses\NotStarted;
13
use ByTIC\Payments\Utility\PaymentsModels;
14
use Nip\Utility\Date;
15
16
/**
17
 * Class SubscriptionBuilder
18
 * @package ByTIC\Payments\Utility
19
 */
20
class SubscriptionBuilder
21
{
22
    use Builder\ManageBillingCycle;
23
24
    /**
25
     * @var \ByTIC\Payments\Models\Subscriptions\Subscription
26
     */
27
    protected $subscription;
28
29
    /**
30
     * SubscriptionBuilder constructor.
31
     */
32
    protected function __construct()
33
    {
34
        $this->subscription = PaymentsModels::subscriptions()->getNew();
35
        $this->subscription->setPropertyValue('status', NotStarted::NAME);
0 ignored issues
show
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

35
        $this->subscription->/** @scrutinizer ignore-call */ 
36
                             setPropertyValue('status', NotStarted::NAME);
Loading history...
36
        $this->subscription->charge_method = Internal::NAME;
0 ignored issues
show
Documentation Bug introduced by
The property $charge_method was declared of type integer, but ByTIC\Payments\Subscript...eMethods\Internal::NAME is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
37
        $this->subscription->start_at = Date::now();
38
    }
39
40
    /**
41
     * @param IsPurchasableModelTrait $purchase
42
     */
43
    public static function fromPurchase($purchase): SubscriptionBuilder
44
    {
45
        $builder = new self();
46
        $builder->withPaymentMethod($purchase->getPaymentMethod());
47
        $builder->withCustomer($purchase->getPurchaseBillingRecord());
0 ignored issues
show
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...
48
49
        $transaction = PaymentsModels::transactions()->findOrCreateForPurchase($purchase);
50
        $builder->withLastTransaction($transaction);
51
        return $builder;
52
    }
53
54
    /**
55
     * @param $status
56
     * @return SubscriptionBuilder
57
     */
58
    public function withStatus($status): SubscriptionBuilder
59
    {
60
        $this->subscription->setPropertyValue('status', $status);
61
        return $this;
62
    }
63
64
    /**
65
     * @param PaymentMethod|PaymentMethodTrait $method
66
     */
67
    public function withPaymentMethod($method): SubscriptionBuilder
68
    {
69
        $this->subscription->populateFromPaymentMethod($method);
70
        return $this;
71
    }
72
73
    /**
74
     * @param Transaction|TransactionTrait $transaction
75
     */
76
    public function withLastTransaction($transaction): SubscriptionBuilder
77
    {
78
        $this->subscription->populateFromLastTransaction($transaction);
79
        return $this;
80
    }
81
82
    /**
83
     * @param BillingRecord $customer
84
     */
85
    public function withCustomer($customer): SubscriptionBuilder
86
    {
87
        $this->subscription->populateFromCustomer($customer);
88
        return $this;
89
    }
90
91
    /**
92
     * @return \ByTIC\Payments\Models\Subscriptions\Subscription|\ByTIC\Payments\Models\Subscriptions\SubscriptionTrait
93
     */
94
    public function create()
95
    {
96
        $this->subscription->insert();
97
98
        $lastTransaction = $this->subscription->getLastTransaction();
99
        $lastTransaction->populateFromSubscription($this->subscription);
100
        $lastTransaction->update();
101
102
        return $this->subscription;
103
    }
104
}
105