Completed
Pull Request — master (#1)
by Dmitry
03:17
created

YandexP2pMerchant   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 26.67 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 2
cbo 6
dl 16
loc 60
ccs 0
cts 28
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A completePurchase() 16 16 1
A createGateway() 0 8 1
A requestPurchase() 0 18 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace hiqdev\php\merchant\merchants\yandex;
4
5
use hiqdev\php\merchant\credentials\CredentialsInterface;
6
use hiqdev\php\merchant\factories\GatewayFactoryInterface;
7
use hiqdev\php\merchant\InvoiceInterface;
8
use hiqdev\php\merchant\merchants\AbstractMerchant;
9
use hiqdev\php\merchant\merchants\MerchantInterface;
10
use hiqdev\php\merchant\response\CompletePurchaseResponse;
11
use hiqdev\php\merchant\response\RedirectPurchaseResponse;
12
use Money\Currency;
13
use Money\Money;
14
use Money\MoneyFormatter;
15
use Money\MoneyParser;
16
use Omnipay\YandexMoney\P2pGateway;
17
18
/**
19
 * Class YandexP2pMerchant
20
 *
21
 * @author Dmytro Naumenko <[email protected]>
22
 */
23
class YandexP2pMerchant extends AbstractMerchant
24
{
25
    /**
26
     * @var P2pGateway
27
     */
28
    protected $gateway;
29
30
    protected function createGateway()
31
    {
32
        return $this->gatewayFactory->build('YandexMoney_P2p', [
33
            'account' => $this->credentials->getPurse(),
34
            'password' => $this->credentials->getKey1(),
35
            'testMode' => $this->credentials->isTestMode()
36
        ]);
37
    }
38
39
    /**
40
     * @param InvoiceInterface $invoice
41
     * @return RedirectPurchaseResponse
42
     */
43
    public function requestPurchase(InvoiceInterface $invoice)
44
    {
45
        /**
46
         * @var \Omnipay\YandexMoney\Message\p2p\PurchaseResponse $response
47
         */
48
        $response = $this->gateway->purchase([
49
            'transactionId' => $invoice->getId(),
50
            'description' => $invoice->getDescription(),
51
            'amount' => $this->moneyFormatter->format($invoice->getAmount()),
52
            'currency' => $invoice->getCurrency()->getCode(),
53
            'returnUrl' => $invoice->getReturnUrl(),
54
            'notifyUrl' => $invoice->getNotifyUrl(),
55
            'cancelUrl' => $invoice->getCancelUrl(),
56
            'method' => 'PC', // https://money.yandex.ru/doc.xml?id=526991
57
        ])->send();
58
59
        return new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData());
60
    }
61
62
    /**
63
     * @param array $data
64
     * @return CompletePurchaseResponse
65
     */
66 View Code Duplication
    public function completePurchase($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        /** @var \Omnipay\YandexMoney\Message\p2p\CompletePurchaseResponse $response */
69
        $response = $this->gateway->completePurchase($data)->send();
70
71
        return (new CompletePurchaseResponse())
72
            ->setIsSuccessful($response->isSuccessful())
73
            ->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency()))
74
            ->setTransactionReference($response->getTransactionReference())
75
            ->setTransactionId($response->getTransactionId())
76
            ->setPayer($response->getData()['sender'] ?? $response->getData()['email'] ?? '')
77
            ->setTime(
78
                (new \DateTime($response->getTime(), new \DateTimeZone('Europe/Moscow')))
79
                    ->setTimezone(new \DateTimeZone('UTC'))
80
            );
81
    }
82
}
83