Completed
Push — master ( 259fd8...a43b07 )
by Andrii
03:33
created

src/merchants/bitpay/BitPayMerchant.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Generalization over Omnipay and Payum
4
 *
5
 * @link      https://github.com/hiqdev/php-merchant
6
 * @package   php-merchant
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\merchant\merchants\bitpay;
12
13
use hiqdev\php\merchant\InvoiceInterface;
14
use hiqdev\php\merchant\merchants\AbstractMerchant;
15
use hiqdev\php\merchant\response\CompletePurchaseResponse;
16
use hiqdev\php\merchant\response\RedirectPurchaseResponse;
17
use Omnipay\BitPay\Gateway;
18
19
/**
20
 * Class BitPayAdapter.
21
 *
22
 * @author Dmytro Naumenko <[email protected]>
23
 */
24
class BitPayMerchant extends AbstractMerchant
25
{
26
    /**
27
     * @return Gateway
28
     */
29 3
    protected function createGateway()
30
    {
31 3
        return $this->gatewayFactory->build('BitPay', [
32 3
            'token' => $this->credentials->getKey1(),
33 3
            'privateKey'  => $this->credentials->getKey2(),
34 3
            'publicKey' => $this->credentials->getKey3(),
35 3
            'testMode' => $this->getCredentials()->isTestMode(),
36
        ]);
37
    }
38
39
    /**
40
     * @param InvoiceInterface $invoice
41
     * @return RedirectPurchaseResponse
42
     */
43 1
    public function requestPurchase(InvoiceInterface $invoice)
44
    {
45
        /**
46
         * @var \Omnipay\BitPay\Message\PurchaseResponse
47
         */
48 1
        $response = $this->gateway->purchase([
0 ignored issues
show
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\FreeKassa\Gateway, Omnipay\InterKassa\Gateway, Omnipay\OKPAY\Gateway, Omnipay\Paxum\Gateway, Omnipay\PayPal\Gateway, Omnipay\RoboKassa\Gateway, Omnipay\WebMoney\Gateway, Omnipay\YandexMoney\P2pGateway, 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...
49 1
            'transactionId' => $invoice->getId(),
50 1
            'description' => $invoice->getDescription(),
51 1
            'amount' => $this->moneyFormatter->format($invoice->getAmount()),
52 1
            'currency' => $invoice->getCurrency()->getCode(),
53 1
            'returnUrl' => $invoice->getReturnUrl(),
54 1
            'notifyUrl' => $invoice->getNotifyUrl(),
55 1
            'cancelUrl' => $invoice->getCancelUrl(),
56 1
        ])->send();
57
58 1
        $response = new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData());
59 1
        $response->setMethod('GET');
60
61 1
        return $response;
62
    }
63
64
    /**
65
     * @param array $data
66
     * @return CompletePurchaseResponse
67
     */
68 1
    public function completePurchase($data)
69
    {
70
        /** @var \Omnipay\BitPay\Message\CompletePurchaseResponse $response */
71 1
        $response = $this->gateway->completePurchase($data)->send();
0 ignored issues
show
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\FreeKassa\Gateway, Omnipay\InterKassa\Gateway, Omnipay\OKPAY\Gateway, Omnipay\Paxum\Gateway, Omnipay\PayPal\Gateway, Omnipay\RoboKassa\Gateway, Omnipay\WebMoney\Gateway, Omnipay\YandexMoney\P2pGateway, 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...
72
73 1
        return (new CompletePurchaseResponse())
74 1
            ->setIsSuccessful($response->isSuccessful())
75 1
            ->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency()))
76 1
            ->setFee($this->moneyParser->parse($response->getFee(), $response->getCurrency()))
77 1
            ->setTransactionReference($response->getTransactionReference())
78 1
            ->setTransactionId($response->getTransactionId())
79 1
            ->setPayer($response->getPayer())
80 1
            ->setTime(new \DateTime($response->getTime()));
81
    }
82
}
83