GatewayAdapter::getCancelAndReturnUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @author Rafał Muszyński <[email protected]>
5
 * @copyright 2015 Sourcefabric z.ú.
6
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
7
 */
8
namespace Newscoop\PaywallBundle\Adapter;
9
10
use Newscoop\PaywallBundle\Entity\OrderInterface;
11
use Symfony\Component\Routing\RouterInterface;
12
13
/**
14
 * Omnipay adapter.
15
 */
16
class GatewayAdapter
17
{
18
    /**
19
     * Router.
20
     *
21
     * @var RouterInterface
22
     */
23
    protected $router;
24
25
    /**
26
     * Gateway.
27
     *
28
     * @var \Omnipay\Common\GatewayInterface|null
29
     */
30
    protected $gateway;
31
32
    /**
33
     * Construct.
34
     *
35
     * @param RouterInterface                       $router
36
     * @param \Omnipay\Common\GatewayInterface|null $gateway
37
     */
38
    public function __construct(RouterInterface $router, $gateway = null)
39
    {
40
        $this->router = $router;
41
        $this->gateway = $gateway;
42
    }
43
44
    /**
45
     * Purchase action.
46
     *
47
     * @param OrderInterface $order Order
48
     *
49
     * @return object
50
     */
51 View Code Duplication
    public function purchase(OrderInterface $order)
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...
52
    {
53
        if ($this->gateway === null) {
54
            return;
55
        }
56
57
        return $this->gateway->purchase(array_merge(
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\PayPal\ExpressGateway, Omnipay\PayPal\ExpressInContextGateway, Omnipay\PayPal\ProGateway, Omnipay\PayPal\RestGateway.

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...
58
            array(
59
                'amount' => $order->getTotal(),
60
                'currency' => $order->getCurrency(),
61
            ),
62
            $this->getCancelAndReturnUrl()
63
        ))->send();
64
    }
65
66
    /**
67
     * Cplete purchase action.
68
     *
69
     * @param OrderInterface $order Order
70
     *
71
     * @return object
72
     */
73 View Code Duplication
    public function completePurchase(OrderInterface $order)
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...
74
    {
75
        if ($this->gateway === null) {
76
            return;
77
        }
78
79
        return $this->gateway->completePurchase(array_merge(
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\PayPal\ExpressGateway, Omnipay\PayPal\ExpressInContextGateway, Omnipay\PayPal\RestGateway.

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...
80
            array(
81
                'amount' => $order->getTotal(),
82
                'currency' => $order->getCurrency(),
83
            ),
84
            $this->getCancelAndReturnUrl()
85
        ))->send();
86
    }
87
88
    private function getCancelAndReturnUrl()
89
    {
90
        return array(
91
            'cancelUrl' => $this->router->generate('paywall_plugin_purchase_cancel', array(), true),
92
            'returnUrl' => $this->router->generate('paywall_plugin_purchase_return', array(), true),
93
        );
94
    }
95
96
    /**
97
     * Whether offline gateway is set or not.
98
     *
99
     * @return bool
100
     */
101
    public function isOfflineGateway()
102
    {
103
        return $this->gateway ? false : true;
104
    }
105
}
106