InvoiceService::createInvoice()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 55
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 45
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 55
rs 9.2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Service\Paypal;
4
5
use Exception;
6
use PayPal\Api\BillingInfo;
7
use PayPal\Api\Currency;
8
use PayPal\Api\Invoice;
9
use PayPal\Api\InvoiceAddress;
10
use PayPal\Api\InvoiceItem;
11
use PayPal\Api\MerchantInfo;
12
use PayPal\Api\PaymentTerm;
13
use PayPal\Api\ShippingInfo;
14
use PayPal\Api\Tax;
15
16
/**
17
 * Class InvoiceService
18
 * @package App\Service\Paypal
19
 */
20
class InvoiceService extends AbstractPaypalService
21
{
22
    /**
23
     * @param array $invoiceForm
24
     * @return Invoice|null
25
     */
26
    public function createInvoice(array $invoiceForm): ?Invoice
27
    {
28
        $invoice = new Invoice();
29
        $invoice
30
            ->setMerchantInfo(new MerchantInfo())
31
            ->setBillingInfo([(new BillingInfo())])
32
            ->setNote($invoiceForm['note'])
33
            ->setPaymentTerm(new PaymentTerm())
34
            ->setShippingInfo(new ShippingInfo());
35
36
        $invoice->getMerchantInfo()
37
            ->setEmail($invoiceForm['merchant_email'])
38
            ->setbusinessName($invoiceForm['merchant_business_name'])
39
            ->setAddress(new InvoiceAddress());
40
41
        $invoice->getMerchantInfo()->getAddress()
42
            ->setLine1($invoiceForm['merchant_address'])
43
            ->setCity($invoiceForm['merchant_city'])
44
            ->setState($invoiceForm['merchant_state'])
45
            ->setPostalCode($invoiceForm['merchant_zip_code'])
46
            ->setCountryCode($invoiceForm['merchant_country_code']);
47
48
        $billing = $invoice->getBillingInfo();
49
        $billing[0]->setLanguage($this->settingsService->getSetting('settings-customer-locale'))
50
            ->setEmail($this->settingsService->getSetting('settings-customer-email'));
51
        $items = [];
52
        $items[0] = new InvoiceItem();
53
        $items[0]
54
            ->setName($invoiceForm['item_name'])
55
            ->setDescription($invoiceForm['item_description'])
56
            ->setQuantity(1)
57
            ->setUnitPrice(new Currency());
58
        $items[0]->getUnitPrice()
59
            ->setCurrency($this->settingsService->getSetting('settings-customer-currency'))
60
            ->setValue($invoiceForm['item_amount']);
61
62
        $tax = new Tax();
63
        $tax->setPercent($invoiceForm['item_tax_percent'])
64
            ->setName($invoiceForm['item_tax_name']);
65
        $items[0]->setTax($tax);
66
        $invoice->setItems($items);
67
68
        $invoice->getPaymentTerm()
69
            ->setTermType("NET_10");
70
        $invoice->setTaxInclusive(true);
71
        $invoice->setLogoUrl('https://www.paypalobjects.com/webstatic/i/logo/rebrand/ppcom.svg');
72
73
        try {
74
            $invoice->create($this->apiContext);
75
        } catch (Exception $e) {
76
            $this->logger->error('Error on PayPal::createInvoice = ' . $e->getMessage());
77
            return null;
78
        }
79
80
        return $invoice;
81
    }
82
83
    /**
84
     * @param string $invoiceId
85
     * @return Invoice|null
86
     */
87
    public function getInvoice(string $invoiceId): ?Invoice
88
    {
89
        try {
90
            $invoice = Invoice::get($invoiceId, $this->apiContext);
91
        } catch (Exception $e) {
92
            $this->logger->error('Error on PayPal::getInvoice = ' . $e->getMessage());
93
            return null;
94
        }
95
96
        return $invoice;
97
    }
98
99
    /**
100
     * @param Invoice $invoice
101
     * @return string
102
     */
103
    public function getInvoiceQRHTML(Invoice $invoice): string
104
    {
105
        $image = Invoice::qrCode($invoice->getId(), ['height' => '400', 'width' => '400'], $this->apiContext);
106
        return '<img src="data:image/png;base64,' . $image->getImage() . '" alt="Invoice QR Code" />';
107
    }
108
109
    /**
110
     * @param Invoice $invoice
111
     * @return Invoice|null
112
     */
113
    public function sendInvoice(Invoice $invoice): ?Invoice
114
    {
115
        try {
116
            $invoice->send($this->apiContext);
117
        } catch (Exception $e) {
118
            $this->logger->error('Error on PayPal::sendInvoice = ' . $e->getMessage());
119
            return null;
120
        }
121
122
        return $invoice;
123
    }
124
}
125