Completed
Push — core-test-reorganisation ( 880d88 )
by Kamil
44:19 queued 07:25
created

PaymentMethodNameToGatewayConverter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 76
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A convert() 0 6 1
A getRegisteredGateways() 0 4 1
A validate() 0 6 2
A tryToMapPaymentMethodName() 0 13 3
A convertGatewayKeyToName() 0 4 1
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
0 ignored issues
show
Documentation introduced by
Should the return type not be integer?

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.

Loading history...
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