|
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) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($this->gateway === null) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->gateway->purchase(array_merge( |
|
|
|
|
|
|
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) |
|
74
|
|
|
{ |
|
75
|
|
|
if ($this->gateway === null) { |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->gateway->completePurchase(array_merge( |
|
|
|
|
|
|
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
|
|
|
|
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: