Completed
Push — master ( b66904...5478f2 )
by Orkhan
14:02
created

Goldenpay::paymentKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
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\Goldenpay\PaymentKey;
12
use Orkhanahmadov\Goldenpay\PaymentResult;
13
use Orkhanahmadov\LaravelGoldenpay\Models\Payment;
14
15
class Goldenpay
16
{
17
    /**
18
     * @var Application
19
     */
20
    private $application;
21
    /**
22
     * @var Library
23
     */
24
    private $goldenpay;
25
26
    /**
27
     * Goldenpay constructor.
28
     *
29
     * @param Application $application
30
     * @param Repository $config
31
     * @param PaymentInterface $goldenpay
32
     */
33
    public function __construct(Application $application, Repository $config, PaymentInterface $goldenpay)
34
    {
35
        $this->application = $application;
36
        $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...
37
38
        $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...
39
            $config->get('goldenpay.auth_key'),
40
            $config->get('goldenpay.merchant_name')
41
        );
42
    }
43
44
    /**
45
     * @param int $amount
46
     * @param CardType $cardType
47
     * @param string $description
48
     * @param Language|null $lang
49
     *
50
     * @throws \Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException
51
     *
52
     * @return PaymentKey
53
     */
54
    public function paymentKey(int $amount, CardType $cardType, string $description, ?Language $lang = null): PaymentKey
55
    {
56
        $lang = $lang ?: $this->languageFromLocale();
57
58
        $paymentKey = $this->goldenpay->paymentKey($amount, $cardType, $description, $lang);
59
60
        Payment::create([
61
            'payment_key' => $paymentKey->getKey(),
62
            'amount' => $amount,
63
            'card_type' => $cardType->getValue(),
64
            'language' => $lang->getValue(),
65
            'description' => $description,
66
        ]);
67
68
        return $paymentKey;
69
    }
70
71
    public function paymentResult($paymentKey): PaymentResult
72
    {
73
        return $this->goldenpay->paymentResult($paymentKey);
74
    }
75
76
    /**
77
     * @return Language
78
     */
79
    private function languageFromLocale(): Language
80
    {
81
        $currentLocale = strtoupper($this->application->getLocale());
82
83
        if (! in_array($currentLocale, ['EN', 'RU', 'AZ'])) {
84
            return Language::EN();
85
        }
86
87
        return Language::{$currentLocale}();
88
    }
89
}
90