Completed
Push — master ( a032e9...21f152 )
by Dmitry
14:20
created

OmnipayMerchant   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 13 2
A response() 0 11 2
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\bitpay;
12
13
use hiqdev\php\merchant\Helper;
14
use hiqdev\php\merchant\RequestInterface;
15
use Omnipay\BitPay\Message\PurchaseRequest;
16
use Omnipay\BitPay\Message\PurchaseResponse;
17
18
/**
19
 * BitPay Omnipay Merchant class.
20
 */
21
class OmnipayMerchant extends \hiqdev\php\merchant\OmnipayMerchant
22
{
23
    public function request($type, array $data)
24
    {
25
        if (!empty($data['inputs'])) {
26
            return Helper::createObject([
27
                'class' => FakeRequest::class,
28
                'merchant' => $this,
29
                'type' => $type,
30
                'data' => array_merge((array)$this->data, (array)$data),
31
            ]);
32
        }
33
34
        return parent::request($type, $data);
35
    }
36
37
    public function response(RequestInterface $request)
38
    {
39
        if ($request instanceof FakeRequest) {
40
            /** @var PurchaseRequest $realRequest */
41
            $realRequest = $request->merchant->getWorker()->purchase($this->data);
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\Stripe\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...
42
43
            return new PurchaseResponse($realRequest, $this->data['inputs']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Omnipay\BitP...$this->data['inputs']); (Omnipay\BitPay\Message\PurchaseResponse) is incompatible with the return type of the parent method hiqdev\php\merchant\AbstractMerchant::response of type hiqdev\php\merchant\AbstractResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
44
        }
45
46
        return parent::response($request);
47
    }
48
}
49