Completed
Pull Request — master (#1)
by Dmitry
11:43 queued 10:09
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
lcom 2
cbo 6
dl 16
loc 60
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createGateway() 0 8 1
A requestPurchase() 0 18 1
A completePurchase() 16 16 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\InvoiceInterface;
6
use hiqdev\php\merchant\merchants\AbstractMerchant;
7
use hiqdev\php\merchant\response\CompletePurchaseResponse;
8
use hiqdev\php\merchant\response\RedirectPurchaseResponse;
9
use Omnipay\YandexMoney\P2pGateway;
10
11
/**
12
 * Class YandexP2pMerchant
13
 *
14
 * @author Dmytro Naumenko <[email protected]>
15
 */
16
class YandexP2pMerchant extends AbstractMerchant
17
{
18
    /**
19
     * @var P2pGateway
20
     */
21
    protected $gateway;
22
23
    protected function createGateway()
24
    {
25
        return $this->gatewayFactory->build('YandexMoney_P2p', [
26
            'account' => $this->credentials->getPurse(),
27
            'password' => $this->credentials->getKey1(),
28
            'testMode' => $this->credentials->isTestMode()
29
        ]);
30
    }
31
32
    /**
33
     * @param InvoiceInterface $invoice
34
     * @return RedirectPurchaseResponse
35
     */
36
    public function requestPurchase(InvoiceInterface $invoice)
37
    {
38
        /**
39
         * @var \Omnipay\YandexMoney\Message\p2p\PurchaseResponse $response
40
         */
41
        $response = $this->gateway->purchase([
42
            'transactionId' => $invoice->getId(),
43
            'description' => $invoice->getDescription(),
44
            'amount' => $this->moneyFormatter->format($invoice->getAmount()),
45
            'currency' => $invoice->getCurrency()->getCode(),
46
            'returnUrl' => $invoice->getReturnUrl(),
47
            'notifyUrl' => $invoice->getNotifyUrl(),
48
            'cancelUrl' => $invoice->getCancelUrl(),
49
            'method' => 'PC', // https://money.yandex.ru/doc.xml?id=526991
50
        ])->send();
51
52
        return new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData());
53
    }
54
55
    /**
56
     * @param array $data
57
     * @return CompletePurchaseResponse
58
     */
59 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...
60
    {
61
        /** @var \Omnipay\YandexMoney\Message\p2p\CompletePurchaseResponse $response */
62
        $response = $this->gateway->completePurchase($data)->send();
63
64
        return (new CompletePurchaseResponse())
65
            ->setIsSuccessful($response->isSuccessful())
66
            ->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency()))
67
            ->setTransactionReference($response->getTransactionReference())
68
            ->setTransactionId($response->getTransactionId())
69
            ->setPayer($response->getData()['sender'] ?? $response->getData()['email'] ?? '')
70
            ->setTime(
71
                (new \DateTime($response->getTime(), new \DateTimeZone('Europe/Moscow')))
72
                    ->setTimezone(new \DateTimeZone('UTC'))
73
            );
74
    }
75
}
76