Completed
Push — master ( f5fbad...00036d )
by Iurii
01:34
created

OmnipayLibrary::hookLibraryList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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