Completed
Push — master ( 6dc05b...a32ac7 )
by Iurii
01:08
created

Main::hookModuleDisableAfter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Omnipay Library
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2015, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3
8
 */
9
10
namespace gplcart\modules\omnipay_library;
11
12
/**
13
 * Main class for Omnipay Library module
14
 */
15
class Main
16
{
17
18
    /**
19
     * Implements hook "library.list"
20
     * @param array $libraries
21
     */
22
    public function hookLibraryList(array &$libraries)
23
    {
24
        $libraries['omnipay'] = array(
25
            'name' => 'Omnipay', // @text
26
            'description' => 'A framework agnostic, multi-gateway payment processing library for PHP 5.3+', // @text
27
            'url' => 'https://github.com/thephpleague/omnipay',
28
            'download' => 'https://github.com/thephpleague/omnipay-common/archive/2.5.2.zip',
29
            'type' => 'php',
30
            'version' => '2.5.2',
31
            'module' => 'omnipay_library',
32
            'vendor' => 'omnipay/common',
33
        );
34
    }
35
36
    /**
37
     * Returns an array of gateways extracted from registered namespaces
38
     * @return array
39
     */
40
    public function getGateways()
41
    {
42
        $gateways = array();
43
44
        foreach ($this->getGatewayNamespaces() as $namespace) {
45
46
            if (strpos($namespace, 'Omnipay') !== 0) {
47
                continue;
48
            }
49
50
            $matches = array();
51
            preg_match('/Omnipay\\\(.+?)\\\/', $namespace, $matches);
52
53
            if (isset($matches[1])) {
54
                $gateways[] = $matches[1];
55
            }
56
        }
57
58
        return $gateways;
59
    }
60
61
    /**
62
     * Returns an array of registered gateway instances
63
     * @param null|string $gateway
64
     * @return mixed
65
     */
66
    public function getGatewayInstance($gateway = null)
67
    {
68
        require __DIR__ . '/vendor/autoload.php';
69
70
        foreach ($this->getGateways() as $id) {
71
            $class = \Omnipay\Common\Helper::getGatewayClassName($id);
72
            if (class_exists($class)) {
73
                \Omnipay\Omnipay::register($id);
74
            }
75
        }
76
77
        $instances = array();
78
        foreach (\Omnipay\Omnipay::find() as $id) {
79
            $instances[$id] = \Omnipay\Omnipay::create($id);
80
        }
81
82
        if (isset($gateway)) {
83
            return empty($instances[$gateway]) ? null : $instances[$gateway];
84
        }
85
86
        return $instances;
87
    }
88
89
    /**
90
     * Returns registered namespaces from composer's autoload file
91
     * @return array
92
     */
93
    protected function getGatewayNamespaces()
94
    {
95
        $file = GC_DIR_VENDOR . '/composer/autoload_psr4.php';
96
97
        if (!is_readable($file)) {
98
            return array();
99
        }
100
101
        $namespaces = include $file;
102
        return array_keys($namespaces);
103
    }
104
105
}
106