Passed
Push — v3.0 ( 8df734...660784 )
by Raza
03:01
created

Filters::addInvoiceFilterByCurrencyCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Srmklive\PayPal\Traits\PayPalAPI\InvoiceSearch;
4
5
use Carbon\Carbon;
6
7
trait Filters
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $invoice_search_filters = [];
13
14
    /**
15
     * @var array
16
     */
17
    protected $invoices_date_types = ['invoice_date', 'due_date', 'payment_date', 'creation_date'];
18
19
    /**
20
     * @param string $email
21
     *
22
     * @return \Srmklive\PayPal\Services\PayPal
23
     */
24
    public function addInvoiceFilterByRecipientEmail(string $email)
25
    {
26
        $this->invoice_search_filters['recipient_email'] = $email;
27
28
        return $this;
29
    }
30
31
    /**
32
     * @param string $name
33
     *
34
     * @return \Srmklive\PayPal\Services\PayPal
35
     */
36
    public function addInvoiceFilterByRecipientFirstName(string $name)
37
    {
38
        $this->invoice_search_filters['recipient_first_name'] = $name;
39
40
        return $this;
41
    }
42
43
    /**
44
     * @param string $name
45
     *
46
     * @return \Srmklive\PayPal\Services\PayPal
47
     */
48
    public function addInvoiceFilterByRecipientLastName(string $name)
49
    {
50
        $this->invoice_search_filters['recipient_last_name'] = $name;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @param string $name
57
     *
58
     * @return \Srmklive\PayPal\Services\PayPal
59
     */
60
    public function addInvoiceFilterByRecipientBusinessName(string $name)
61
    {
62
        $this->invoice_search_filters['recipient_business_name'] = $name;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param string $invoice_number
69
     *
70
     * @return \Srmklive\PayPal\Services\PayPal
71
     */
72
    public function addInvoiceFilterByInvoiceNumber(string $invoice_number)
73
    {
74
        $this->invoice_search_filters['invoice_number'] = $invoice_number;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param array $status
81
     *
82
     * @return \Srmklive\PayPal\Services\PayPal
83
     *
84
     * @see https://developer.paypal.com/docs/api/invoicing/v2/#definition-invoice_status
85
     */
86
    public function addInvoiceFilterByInvoiceStatus(array $status)
87
    {
88
        $this->invoice_search_filters['status'] = $status;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param string $reference
95
     * @param bool   $memo
96
     *
97
     * @return \Srmklive\PayPal\Services\PayPal
98
     */
99
    public function addInvoiceFilterByReferenceorMemo(string $reference, bool $memo = false)
100
    {
101
        $field = ($memo === false) ? 'reference' : 'memo';
102
103
        $this->invoice_search_filters[$field] = $reference;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @param string $currency
110
     *
111
     * @return \Srmklive\PayPal\Services\PayPal
112
     */
113
    public function addInvoiceFilterByCurrencyCode(string $currency = '')
114
    {
115
        if (!isset($currency)) {
116
            $currency = $this->getCurrency();
0 ignored issues
show
Bug introduced by
It seems like getCurrency() 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

116
            /** @scrutinizer ignore-call */ 
117
            $currency = $this->getCurrency();
Loading history...
117
        }
118
119
        $this->invoice_search_filters['currency_code'] = $currency;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param float  $start_amount
126
     * @param float  $end_amount
127
     * @param string $amount_currency
128
     *
129
     * @return \Srmklive\PayPal\Services\PayPal
130
     *
131
     * @throws \Exception
132
     */
133
    public function addInvoiceFilterByAmountRange(float $start_amount, float $end_amount, string $amount_currency = '')
134
    {
135
        if ($start_amount > $end_amount) {
136
            throw new \Exception("Starting amount should always be less than end amount!");
137
        }
138
139
        $currency = $this->getCurrency();
140
        if (!isset($amount_currency)) {
141
            $currency = $amount_currency;
142
        } elseif (!empty($this->invoice_search_filters['currency_code'])) {
143
            $currency = $this->invoice_search_filters['currency_code'];
144
        }
145
        
146
        $this->invoice_search_filters['total_amount_range'] = [
147
            'lower_amount' => [
148
                'currency_code' => $currency,
149
                'value'         => $start_amount,
150
            ],
151
            'upper_amount' => [
152
                'currency_code' => $currency,
153
                'value'         => $end_amount,
154
            ],            
155
        ];
156
157
        return $this;
158
    }
159
160
    /**
161
     * @param string $start_date
162
     * @param string $end_date
163
     * @param string $date_type
164
     *
165
     * @return \Srmklive\PayPal\Services\PayPal
166
     *
167
     * @throws \Exception
168
     */
169
    public function addInvoiceFilterByDateRange(string $start_date, string $end_date, string $date_type)
170
    {
171
        $start_date_obj = Carbon::parse($start_date);
172
        $end_date_obj = Carbon::parse($end_date);
173
174
        if ($start_date_obj->gt($end_date_obj)) {
175
            throw new \Exception("Starting date should always be less than the end date!");
176
        }
177
178
        if (!in_array($date_type, $this->invoices_date_types)) {
179
            throw new \Exception("date type should be always one of these: " . implode(',', $this->invoices_date_types));
180
        }
181
182
        $this->invoice_search_filters["{$date_type}_range"] = [
183
            'start' => $start_date,
184
            'end'   => $end_date,
185
        ];
186
187
        return $this;
188
    }
189
190
    /**
191
     * @param bool $archived
192
     *
193
     * @return \Srmklive\PayPal\Services\PayPal
194
     */
195
    public function addInvoiceFilterByArchivedStatus(bool $archived=null)
196
    {
197
        $this->invoice_search_filters['archived'] = $archived;
198
199
        return $this;
200
    }
201
}
202