Passed
Push — master ( 19a915...d9efc8 )
by Gabriel
14:56
created

SubscriptionsTrait::getDefaultChargeMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ByTIC\Payments\Models\Subscriptions;
4
5
use ByTIC\Models\SmartProperties\Properties\Types\Generic as GenericType;
6
use ByTIC\Payments\Models\AbstractModels\HasCustomer\HasCustomerRepository;
7
use ByTIC\Payments\Models\AbstractModels\HasPaymentMethod\HasPaymentMethodRepository;
8
use ByTIC\Payments\Models\AbstractModels\HasToken\HasTokenRepository;
9
use ByTIC\Payments\Subscriptions\ChargeMethods\Internal;
10
use ByTIC\Payments\Utility\PaymentsModels;
11
use Nip\Records\Collections\Collection;
12
13
/**
14
 * Trait SubscriptionsTrait
15
 * @package ByTIC\Payments\Models\Subscriptions
16
 *
17
 * @method SubscriptionTrait|Subscription getNew
18
 */
19
trait SubscriptionsTrait
20
{
21
    use HasCustomerRepository;
22
    use HasTokenRepository;
23
    use HasPaymentMethodRepository;
24
    use \ByTIC\Models\SmartProperties\RecordsTraits\HasStatus\RecordsTrait;
25
26
    /**
27
     * @param int $count
28
     * @return Collection|Subscription[]
29
     */
30
    public function findChargeDue(int $count = 10): Collection
31
    {
32
        $query = $this->paramsToQuery();
0 ignored issues
show
Bug introduced by
It seems like paramsToQuery() 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

32
        /** @scrutinizer ignore-call */ 
33
        $query = $this->paramsToQuery();
Loading history...
33
        $query->where('charge_at IS NOT NULL');
34
        $query->where('charge_at < NOW()');
35
        $query->order(['charge_at', 'ASC']);
36
        $query->limit($count);
37
        return $this->findByQuery($query);
0 ignored issues
show
Bug introduced by
The method findByQuery() does not exist on ByTIC\Payments\Models\Su...ions\SubscriptionsTrait. Did you maybe mean findOneByQuery()? ( Ignorable by Annotation )

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

37
        return $this->/** @scrutinizer ignore-call */ findByQuery($query);

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...
38
    }
39
40
    /**
41
     * Get all the types array
42
     *
43
     * @return GenericType[]|null
44
     */
45
    public function getChargeMethods()
46
    {
47
        return $this->getSmartPropertyItems('ChargeMethods');
48
    }
49
50
    /**
51
     * Get property of a type by name
52
     *
53
     * @param string $name Type name
54
     *
55
     * @return array
56
     */
57
    public function getChargeMethodProperty($name)
58
    {
59
        return $this->getSmartPropertyValues('ChargeMethods', $name);
60
    }
61
62
    protected function initRelations()
63
    {
64
        parent::initRelations();
65
        $this->initRelationsPayments();
66
    }
67
68
    protected function initRelationsPayments()
69
    {
70
        $this->initRelationsTransactions();
71
        $this->initRelationsLastTransaction();
72
        $this->initRelationsPaymentMethod();
73
        $this->initRelationsToken();
74
        $this->initRelationsTokens();
75
    }
76
77
    protected function initRelationsTransactions()
78
    {
79
        $this->hasMany('Transactions', ['class' => get_class(PaymentsModels::transactions())]);
80
    }
81
82
    protected function initRelationsLastTransaction()
83
    {
84
        $this->hasOne('LastTransaction', ['class' => get_class(PaymentsModels::transactions())]);
0 ignored issues
show
Bug introduced by
It seems like hasOne() 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

84
        $this->/** @scrutinizer ignore-call */ 
85
               hasOne('LastTransaction', ['class' => get_class(PaymentsModels::transactions())]);
Loading history...
85
    }
86
87
    protected function initRelationsTokens()
88
    {
89
        $this->hasMany('Tokens', ['class' => get_class(PaymentsModels::tokens())]);
90
    }
91
92
    protected function registerSmartProperties()
93
    {
94
        $this->registerSmartProperty('charge_method', 'ChargeMethods');
95
        $this->registerSmartPropertyStatus();
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getStatusItemsRootNamespace()
102
    {
103
        return 'ByTIC\Payments\Subscriptions\Statuses\\';
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getStatusItemsDirectory()
110
    {
111
        return dirname(dirname(__DIR__))
112
            . DIRECTORY_SEPARATOR . 'Subscriptions'
113
            . DIRECTORY_SEPARATOR . 'Statuses';
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getDefaultChargeMethods()
120
    {
121
        return Internal::NAME;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function getChargeMethodsItemsRootNamespace()
128
    {
129
        return 'ByTIC\Payments\Subscriptions\ChargeMethods\\';
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getChargeMethodsItemsDirectory()
136
    {
137
        return dirname(dirname(__DIR__))
138
            . DIRECTORY_SEPARATOR . 'Subscriptions'
139
            . DIRECTORY_SEPARATOR . 'ChargeMethods';
140
    }
141
142
    /**
143
     * @return mixed|\Nip\Config\Config
144
     * @throws \Exception
145
     */
146
    protected function generateTable()
147
    {
148
        return config('payments.tables.subscriptions', Subscriptions::TABLE);
149
    }
150
}
151