Completed
Push — master ( 5c8cb6...92357d )
by Dmitry
02:05
created

OmnipayMerchant::setGateways()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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;
12
13
use Omnipay\Omnipay;
14
15
/**
16
 * OmnipayMerchant class.
17
 */
18
class OmnipayMerchant extends AbstractMerchant
19
{
20
    public $library = 'Omnipay';
21
22
    /**
23
     * @var \Omnipay\Common\GatewayInterface omnipay Gateway object
24
     */
25
    protected $_worker;
26
27
    /**
28
     * @return \Omnipay\Common\GatewayInterface
29
     */
30
    public function getWorker()
31
    {
32
        if ($this->_worker === null) {
33
            $this->_worker = Omnipay::create($this->getGatewayNamespacePart())->initialize($this->prepareData($this->data));
34
        }
35
36
        return $this->_worker;
37
    }
38
39
    /**
40
     * Returns the normalized gateway name that should be used to create a worker.
41
     *
42
     * @param string $name
43
     * @return string
44
     * @see normalizeGateway
45
     */
46
    public function getGatewayNamespacePart($name = null)
47
    {
48
        if (!isset($name)) {
49
            $name = $this->gateway;
50
        }
51
52
        return $this->normalizeGateway($name);
53
    }
54
55
    /**
56
     * Normalizes gateway $name.
57
     *
58
     * @param $name
59
     * @return string
60
     */
61
    public function normalizeGateway($name)
62
    {
63
        foreach (static::$_gateways as $gateway) {
64
            if (Helper::simplify($gateway) === Helper::simplify($name)) {
65
                return $gateway;
66
            }
67
        }
68
69
        return $name;
70
    }
71
72
    public static $_gateways = [
73
        'eCoin', 'ePayments', 'ePayService', 'InterKassa', 'OKPAY', 'Qiwi',
74
        'Paxum', 'PayPal', 'RoboKassa', 'WebMoney', 'YandexMoney',
75
    ];
76
77
    public static function setGateways($gateways)
78
    {
79
        static::$_gateways = array_merge(static::$_gateways, $gateways);
80
    }
81
82
    /**
83
     * No prepare by default.
84
     * To be redefined in specific gateway merchants.
85
     */
86
    public function prepareData(array $data)
87
    {
88
        return $data;
89
    }
90
}
91