Completed
Push — master ( f0fab6...6c3963 )
by Iurii
01:54
created

OmnipayLibrary   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 139
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A hookLibraryList() 0 15 1
A getGatewayNamespaces() 0 11 2
A getGatewayIds() 0 18 4
B getGatewayInstance() 0 22 6
A hookModuleEnableAfter() 0 4 1
A hookModuleDisableAfter() 0 4 1
A hookModuleInstallAfter() 0 4 1
A hookModuleUninstallAfter() 0 4 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\Module;
13
14
/**
15
 * Main class for Omnipay Library module
16
 */
17
class OmnipayLibrary extends Module
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
        parent::__construct();
33
34
        $this->library = $library;
0 ignored issues
show
Documentation Bug introduced by
It seems like $library of type object<gplcart\modules\omnipay_library\Library> is incompatible with the declared type object<gplcart\core\Library> of property $library.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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