1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Behat\Service; |
13
|
|
|
|
14
|
|
|
use Payum\Core\GatewayInterface; |
15
|
|
|
use Payum\Core\Registry\RegistryInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class PaymentMethodNameToGatewayConverter implements PaymentMethodNameToGatewayConverterInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var RegistryInterface |
24
|
|
|
*/ |
25
|
|
|
private $payumRegistry; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param RegistryInterface $payumRegistry |
29
|
|
|
*/ |
30
|
|
|
public function __construct(RegistryInterface $payumRegistry) |
31
|
|
|
{ |
32
|
|
|
$this->payumRegistry = $payumRegistry; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function convert($paymentMethodName) |
39
|
|
|
{ |
40
|
|
|
$this->validate($paymentMethodName); |
41
|
|
|
|
42
|
|
|
return $this->tryToMapPaymentMethodName($paymentMethodName); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return GatewayInterface[] |
47
|
|
|
*/ |
48
|
|
|
private function getRegisteredGateways() |
49
|
|
|
{ |
50
|
|
|
return $this->payumRegistry->getGateways(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $paymentMethodName |
55
|
|
|
* |
56
|
|
|
* @throws \InvalidArgumentException |
57
|
|
|
*/ |
58
|
|
|
private function validate($paymentMethodName) |
59
|
|
|
{ |
60
|
|
|
if (null == $paymentMethodName) { |
61
|
|
|
throw new \InvalidArgumentException(sprintf('Payment method name cannot be null')); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $paymentMethodName |
67
|
|
|
* |
68
|
|
|
* @throws \RuntimeException |
69
|
|
|
* |
70
|
|
|
* @return string |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
private function tryToMapPaymentMethodName($paymentMethodName) |
73
|
|
|
{ |
74
|
|
|
$paymentMethodName = strtolower($paymentMethodName); |
75
|
|
|
$gateways = $this->getRegisteredGateways(); |
76
|
|
|
|
77
|
|
|
foreach ($gateways as $gatewayKey => $gateway) { |
78
|
|
|
if ($this->convertGatewayKeyToName($gatewayKey) === $paymentMethodName) { |
79
|
|
|
return $gatewayKey; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new \RuntimeException(sprintf('Cannot convert %s to gateway', $paymentMethodName)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $gatewayKey |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
private function convertGatewayKeyToName($gatewayKey) |
92
|
|
|
{ |
93
|
|
|
return str_replace('_', ' ', $gatewayKey); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.