Completed
Push — master ( d5595a...9bb41e )
by Orkhan
08:19
created

Goldenpay::payment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
namespace Orkhanahmadov\LaravelGoldenpay;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\Foundation\Application;
7
use Orkhanahmadov\Goldenpay\Enums\CardType;
8
use Orkhanahmadov\Goldenpay\Enums\Language;
9
use Orkhanahmadov\Goldenpay\Goldenpay as Library;
10
use Orkhanahmadov\Goldenpay\PaymentInterface;
11
use Orkhanahmadov\LaravelGoldenpay\Models\Payment;
12
13
class Goldenpay
14
{
15
    /**
16
     * @var Application
17
     */
18
    private $application;
19
    /**
20
     * @var Library
21
     */
22
    private $goldenpay;
23
24
    /**
25
     * Goldenpay constructor.
26
     *
27
     * @param Application $application
28
     * @param Repository $config
29
     * @param PaymentInterface $goldenpay
30
     */
31
    public function __construct(Application $application, Repository $config, PaymentInterface $goldenpay)
32
    {
33
        $this->application = $application;
34
        $this->goldenpay = $goldenpay;
0 ignored issues
show
Documentation Bug introduced by
$goldenpay is of type object<Orkhanahmadov\Goldenpay\PaymentInterface>, but the property $goldenpay was declared to be of type object<Orkhanahmadov\Goldenpay\Goldenpay>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
35
36
        $this->goldenpay = $goldenpay->auth(
0 ignored issues
show
Documentation Bug introduced by
It seems like $goldenpay->auth($config...denpay.merchant_name')) of type object<self> is incompatible with the declared type object<Orkhanahmadov\Goldenpay\Goldenpay> of property $goldenpay.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
            $config->get('goldenpay.auth_key'),
38
            $config->get('goldenpay.merchant_name')
39
        );
40
    }
41
42
    /**
43
     * @param int $amount
44
     * @param CardType $cardType
45
     * @param string $description
46
     * @param Language|null $lang
47
     *
48
     * @throws \Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException
49
     *
50
     * @return Payment
51
     */
52
    public function payment(int $amount, CardType $cardType, string $description, ?Language $lang = null): Payment
53
    {
54
        $lang = $lang ?: $this->languageFromLocale();
55
56
        $paymentKey = $this->goldenpay->payment($amount, $cardType, $description, $lang);
57
58
        return Payment::create([
59
            'payment_key' => $paymentKey->getPaymentKey(),
60
            'amount' => $amount,
61
            'card_type' => $cardType->getValue(),
62
            'language' => $lang->getValue(),
63
            'description' => $description,
64
        ]);
65
    }
66
67
    /**
68
     * @param Payment|string $payment
69
     *
70
     * @return Payment
71
     */
72
    public function result($payment): Payment
73
    {
74
        if (! $payment instanceof Payment) {
75
            $payment = Payment::wherePaymentKey($payment)->firstOrFail();
76
        }
77
78
        $result = $this->goldenpay->result($payment->payment_key);
79
80
        $payment->status = $result->getCode();
81
        $payment->message = $result->getMessage();
82
        $payment->reference_number = $result->getReferenceNumber();
83
        $payment->card_number = $result->getCardNumber();
84
        $payment->payment_date = $result->getPaymentDate();
85
        $payment->checks = $result->getCheckCount();
86
        $payment->save();
87
88
        return $payment;
89
    }
90
91
    /**
92
     * @return Language
93
     */
94
    private function languageFromLocale(): Language
95
    {
96
        $currentLocale = strtoupper($this->application->getLocale());
97
98
        if (! in_array($currentLocale, ['EN', 'RU', 'AZ'])) {
99
            return Language::EN();
100
        }
101
102
        return Language::{$currentLocale}();
103
    }
104
}
105