Completed
Push — master ( c3b028...fa0517 )
by Iurii
01:16
created

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