Completed
Push — master ( 189007...259fd8 )
by Dmitry
08:51
created

OkpayMerchant   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 2
cbo 8
dl 57
loc 57
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createGateway() 8 8 1
A requestPurchase() 17 17 1
A completePurchase() 14 14 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\okpay;
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
10
/**
11
 * Class OkpayMerchant
12
 *
13
 * @author Dmytro Naumenko <[email protected]>
14
 */
15 View Code Duplication
class OkpayMerchant extends AbstractMerchant
0 ignored issues
show
Duplication introduced by
This class 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...
16
{
17
    /**
18
     * @var \Omnipay\Common\GatewayInterface
19
     */
20
    protected $gateway;
21
22
    protected function createGateway()
23
    {
24
        return $this->gatewayFactory->build('OKPAY', [
25
            'purse' => $this->credentials->getPurse(),
26
            'secret'  => $this->credentials->getKey1(),
27
            'secret2' => $this->credentials->getKey2(),
28
        ]);
29
    }
30
31
    /**
32
     * @param InvoiceInterface $invoice
33
     * @return RedirectPurchaseResponse
34
     */
35
    public function requestPurchase(InvoiceInterface $invoice)
36
    {
37
        /**
38
         * @var \Omnipay\BitPay\Message\PurchaseResponse $response
39
         */
40
        $response = $this->gateway->purchase([
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Omnipay\Common\GatewayInterface as the method purchase() does only exist in the following implementations of said interface: Omnipay\BitPay\Gateway, Omnipay\InterKassa\Gateway, Omnipay\OKPAY\Gateway, Omnipay\Paxum\Gateway, Omnipay\PayPal\Gateway, Omnipay\ePayService\Gateway.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
41
            'transactionId' => $invoice->getId(),
42
            'description' => $invoice->getDescription(),
43
            'amount' => $this->moneyFormatter->format($invoice->getAmount()),
44
            'currency' => $invoice->getCurrency()->getCode(),
45
            'returnUrl' => $invoice->getReturnUrl(),
46
            'notifyUrl' => $invoice->getNotifyUrl(),
47
            'cancelUrl' => $invoice->getCancelUrl(),
48
        ])->send();
49
50
        return new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData());
51
    }
52
53
    /**
54
     * @param array $data
55
     * @return CompletePurchaseResponse
56
     */
57
    public function completePurchase($data)
58
    {
59
        /** @var \Omnipay\OKPAY\Message\CompletePurchaseResponse $response */
60
        $response = $this->gateway->completePurchase($data)->send();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Omnipay\Common\GatewayInterface as the method completePurchase() does only exist in the following implementations of said interface: Omnipay\BitPay\Gateway, Omnipay\InterKassa\Gateway, Omnipay\OKPAY\Gateway, Omnipay\Paxum\Gateway, Omnipay\PayPal\Gateway, Omnipay\ePayService\Gateway.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
61
62
        return (new CompletePurchaseResponse())
63
            ->setIsSuccessful($response->isSuccessful())
0 ignored issues
show
Documentation introduced by
$response->isSuccessful() is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
            ->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency()))
65
            ->setFee($this->moneyParser->parse($response->getFee(), $response->getCurrency()))
66
            ->setTransactionReference($response->getTransactionReference())
67
            ->setTransactionId($response->getTransactionId())
68
            ->setPayer($response->getPayer())
69
            ->setTime($response->getTime());
70
    }
71
}
72