Completed
Push — master ( 738a7a...c2f0e2 )
by Iurii
01:34
created

OmnipayLibrary::getGatewayInstance()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 12
nc 18
nop 1
1
<?php
2
3
/**
4
 * @package GPL Cart core
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
use gplcart\core\Library;
13
use gplcart\core\models\Language as LanguageModel;
14
15
/**
16
 * Main class for Omnipay Library module
17
 */
18
class OmnipayLibrary
19
{
20
21
    /**
22
     * Library class instance
23
     * @var \gplcart\core\Library $library
24
     */
25
    protected $library;
26
27
    /**
28
     * Language model instance
29
     * @var \gplcart\core\models\Language $language
30
     */
31
    protected $language;
32
33
    /**
34
     * Constructor
35
     * @param Library $library
36
     * @param LanguageModel $language
37
     */
38
    public function __construct(Library $library, LanguageModel $language)
39
    {
40
        $this->library = $library;
41
        $this->language = $language;
42
    }
43
44
    /**
45
     * Module info
46
     * @return array
47
     */
48
    public function info()
49
    {
50
        return array(
51
            'core' => '1.x',
52
            'version' => '1.0.0-alfa.2',
53
            'author' => 'Iurii Makukh',
54
            'name' => 'Omnipay library',
55
            'description' => 'A helper module that just provides <a href="https://github.com/thephpleague/omnipay">Omnipay</a> library'
56
        );
57
    }
58
59
    /**
60
     * Implements hook "library.config"
61
     * @param array $configs
62
     */
63
    public function hookLibraryConfig(array &$configs)
64
    {
65
        $configs['omnipay/common']['omnipay'] = array(
66
            'name' => 'Omnipay',
67
            'description' => 'A framework agnostic, multi-gateway payment processing library for PHP 5.3+',
68
            'url' => 'https://github.com/thephpleague/omnipay',
69
            'download' => 'https://github.com/thephpleague/omnipay-common/archive/2.5.2.zip',
70
            'type' => 'php',
71
            'version' => '2.5.2',
72
            'module' => 'omnipay_library',
73
            'files' => array(
74
                'vendor/autoload.php'
75
            )
76
        );
77
    }
78
79
    /**
80
     * Retuns registered namespaces from composer's autoload file
81
     * @return array
82
     */
83
    protected function getGatewayNamespaces()
84
    {
85
        $file = __DIR__ . '/vendor/composer/autoload_psr4.php';
86
        if (is_readable($file)) {
87
            $namespaces = include $file;
88
            return array_keys($namespaces);
89
        }
90
        return array();
91
    }
92
93
    /**
94
     * Returns an array of gateways extracted from registered namespaces
95
     * @return array
96
     */
97
    public function getGatewayIds()
98
    {
99
        $gateways = array();
100
101
        foreach ($this->getGatewayNamespaces() as $namespace) {
102
            if (strpos($namespace, 'Omnipay') !== 0) {
103
                continue;
104
            }
105
            $matches = array();
106
            preg_match('/Omnipay\\\(.+?)\\\/', $namespace, $matches);
107
108
            if (isset($matches[1])) {
109
                $gateways[] = $matches[1];
110
            }
111
        }
112
113
        return $gateways;
114
    }
115
116
    /**
117
     * Returns an array of registered gateway instances
118
     * @return null|object|array
119
     */
120
    public function getGatewayInstance($gateway = null)
121
    {
122
        $this->library->load('omnipay');
123
124
        foreach ($this->getGatewayIds() as $id) {
125
            $class = \Omnipay\Common\Helper::getGatewayClassName($id);
126
            if (class_exists($class)) {
127
                \Omnipay\Omnipay::register($id);
128
            }
129
        }
130
131
        $instances = array();
132
        foreach (\Omnipay\Omnipay::find() as $id) {
133
            $instances[$id] = \Omnipay\Omnipay::create($id);
134
        }
135
136
        if (isset($gateway)) {
137
            return empty($instances[$gateway]) ? null : $instances[$gateway];
138
        }
139
140
        return $instances;
141
    }
142
143
    /**
144
     * Implements hook "module.enable.after"
145
     */
146
    public function hookModuleEnableAfter()
147
    {
148
        $this->library->clearCache();
149
    }
150
151
    /**
152
     * Implements hook "module.disable.after"
153
     */
154
    public function hookModuleDisableAfter()
155
    {
156
        $this->library->clearCache();
157
    }
158
159
    /**
160
     * Implements hook "module.install.after"
161
     */
162
    public function hookModuleInstallAfter()
163
    {
164
        $this->library->clearCache();
165
    }
166
167
    /**
168
     * Implements hook "module.uninstall.after"
169
     */
170
    public function hookModuleUninstallAfter()
171
    {
172
        $this->library->clearCache();
173
    }
174
175
}
176