Completed
Push — master ( 00e474...9d3fbd )
by Michael
04:26
created

Oledrion_gateways::gatewayClassExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 33 and the first side effect is on line 28.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * oledrion
14
 *
15
 * @copyright   {@link http://xoops.org/ XOOPS Project}
16
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
17
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
18
 */
19
20
/**
21
 * Classe chargée de la manipulation des passerelles de paiement
22
 *
23
 * Normalement la classe est utilisable de manière statique
24
 *
25
 */
26
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
27
28
require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
29
30
/**
31
 * Class Oledrion_gateways
32
 */
33
class Oledrion_gateways
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
34
{
35
    /**
36
     * Retourne la passerelle de paiement en cours d'utilisation
37
     *
38
     * @param  null $gateway
39
     * @return string Le nom de la  passerelle de paiement (en fait le nom de son répertoire)
40
     */
41
    public static function getCurrentGateway($gateway = null)
42
    {
43
        if ($gateway) {
44
            $return = $gateway;
45
        } else {
46
            $return = xoops_trim(Oledrion_utils::getModuleOption('used_gateway'));
47
        }
48
49
        if ($return == '') {
50
            $return = 'paypal'; // Valeur par défaut
51
        }
52
53
        return $return;
54
    }
55
56
    /**
57
     * Retourne la passerelle de paiement en cours d'utilisation
58
     *
59
     * @return string Le nom de la  passerelle de paiement (en fait le nom de son répertoire)
60
     */
61
    public static function getDefaultGateway()
62
    {
63
        $return = xoops_trim(Oledrion_utils::getModuleOption('used_gateway'));
64
        if ($return == '') {
65
            $return = 'paypal'; // Valeur par défaut
66
        }
67
68
        return $return;
69
    }
70
71
    /**
72
     * Nettoie le nom de la passerelle de paiement
73
     *
74
     * @param  string $gatewayName Le nom de la  passerelle de paiement
75
     * @return string
76
     */
77
    public static function purifyGatewayName($gatewayName)
78
    {
79
        return str_replace('..', '', $gatewayName);
80
    }
81
82
    /**
83
     * Retourne la liste des passerelles de paiement installées
84
     *
85
     * @return array
86
     */
87
    public static function getInstalledGatewaysList()
88
    {
89
        return XoopsLists::getDirListAsArray(OLEDRION_ADMIN_PATH . 'gateways/');
90
    }
91
92
    /**
93
     * Retourne le chemin d'accès à une passerelle de paiement
94
     *
95
     * @param  string $gatewayName Le nom de la  passerelle de paiement (son répertoire)
96
     * @return string
97
     */
98
    public static function getGatewayPath($gatewayName)
99
    {
100
        return OLEDRION_ADMIN_PATH . 'gateways' . '/' . $gatewayName; // Par exemple c:/inetpub/wwwroot/xoops/modules/oledrion/admin/gateways/paypal
101
    }
102
103
    /**
104
     * Retourne le chemin complet vers le fichier de langue de la passerelle
105
     *
106
     * @param  unknown_type $gatewayName
107
     * @return unknown
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
108
     */
109
    public static function getGatewayLanguageFilename($gatewayName)
110
    {
111
        global $xoopsConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
112
        $gatewayPath = self::getGatewayPath($gatewayName);
113
114
        return $gatewayPath . '/language/' . $xoopsConfig['language'] . '/main.php';
115
    }
116
117
    /**
118
     * Charge le fichier de traductions d'une passerelle de paiement
119
     *
120
     * @param  string $gatewayName      Le nom de la  passerelle de paiement (son répertoire)
121
     * @param  string $languageFilename Utilisé pour retourner le nom du fichier de langue inclu
0 ignored issues
show
Documentation introduced by
Should the type for parameter $languageFilename not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
122
     * @param  bool   $includeIt
123
     * @return bool   True si le chargement a réussi sinon Faux
124
     */
125
    public static function loadGatewaysLanguageDefines($gatewayName, &$languageFilename = null, $includeIt = true)
126
    {
127
        $gatewayPath          = self::getGatewayPath($gatewayName);
128
        $languageFileIncluded = false;
129
        $languageFile         = self::getGatewayLanguageFilename($gatewayName);
0 ignored issues
show
Documentation introduced by
$gatewayName is of type string, but the function expects a object<unknown_type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
        $defaultLanguageFile  = $gatewayPath . '/language/english/main.php';
131
        if (file_exists($languageFile)) {
132
            if ($includeIt) {
133
                require_once $languageFile;
134
            }
135
            $languageFileIncluded = true;
136
            $languageFilename     = $languageFile;
137
        } elseif (file_exists($defaultLanguageFile)) {
138
            $languageFileIncluded = true;
139
            if ($includeIt) {
140
                require_once $defaultLanguageFile;
141
            }
142
            $languageFilename = $defaultLanguageFile;
143
        }
144
145
        return $languageFileIncluded;
146
    }
147
148
    /**
149
     * Retourne le chemin d'accès complet à une passerelle de paiement
150
     *
151
     * @param  string $gatewayName Le nom de la  passerelle de paiement (son répertoire)
152
     * @return string
153
     */
154
    public static function getGatewayFullClassPath($gatewayName)
155
    {
156
        $gatewayPath = self::getGatewayPath($gatewayName);
157
158
        return $gatewayPath . '/gateway.php';
159
    }
160
161
    /**
162
     * Indique si le fichier contenant la classe d'une passerelle de paiement existe
163
     *
164
     * @param  string $gatewayName Le nom de la  passerelle de paiement (son répertoire)
165
     * @return boolean True si le fichier de la classe existe sinon Faux
166
     */
167
    public static function gatewayClassFileExists($gatewayName)
168
    {
169
        $gatewayClassPath = self::getGatewayFullClassPath($gatewayName);
170
        if (file_exists($gatewayClassPath)) {
171
            return true;
172
        } else {
173
            return false;
174
        }
175
    }
176
177
    /**
178
     * Chargement (inclusion) du fichier de la classe de la passerelle de paiement
179
     *
180
     * @param string $gatewayName
181
     */
182
    public static function includeGatewayClass($gatewayName)
183
    {
184
        $gatewayClassPath = self::getGatewayFullClassPath($gatewayName);
185
        require_once $gatewayClassPath;
186
    }
187
188
    /**
189
     * Retourne le nom de la classe attendu pour une passerelle de paiement
190
     *
191
     * @param  string $gatewayName Le nom de la  passerelle de paiement (son répertoire)
192
     * @return string
193
     */
194
    public static function gatewayClassName($gatewayName)
195
    {
196
        return 'oledrion_' . $gatewayName;
197
    }
198
199
    /**
200
     * Indique si la classe de la passerelle de paiement existe
201
     *
202
     * @param  string $gatewayName Le nom de la  passerelle de paiement (son répertoire)
203
     * @return boolean
204
     */
205
    public static function gatewayClassExists($gatewayName)
206
    {
207
        $gatewayClassName = self::gatewayClassName($gatewayName);
208
        if (class_exists($gatewayClassName)) {
209
            return true;
210
        } else {
211
            return false;
212
        }
213
    }
214
215
    /**
216
     * Indique si un objet de type gateway étend bien la classe abstraite
217
     *
218
     * @param  object $gateway L'objet à vérifier
219
     * @return boolean
220
     */
221
    public static function asGoodAncestor($gateway)
222
    {
223
        if (get_parent_class($gateway) === 'oledrion_gateway') {
224
            return true;
225
        } else {
226
            return false;
227
        }
228
    }
229
230
    /**
231
     * Indique si Le nom de la  passerelle de paiement se trouve sur le site
232
     *
233
     * @param  string $gatewayName Le nom de la  passerelle de paiement
234
     * @return boolean
235
     */
236
    public static function isInstalledGatewayName($gatewayName)
237
    {
238
        $installedGateways = self::getInstalledGatewaysList();
239
        if (!in_array($gatewayName, $installedGateways)) {
240
            return false;
241
        } else {
242
            return true;
243
        }
244
    }
245
246
    /**
247
     * Raccourcis pour récupérer l'objet gateway courant
248
     *
249
     * @param  null $gateway
250
     * @return mixed Soit l'objet gateway soit null
251
     *
252
     */
253
    public static function getGatewayObject($gateway = null)
254
    {
255
        $gateway = self::getCurrentGateway($gateway);
256
        if (self::isInstalledGatewayName($gateway)) {
257
            if (self::gatewayClassFileExists($gateway)) {
258
                if (self::loadGatewaysLanguageDefines($gateway)) {
259
                    self::includeGatewayClass($gateway);
260
                    if (self::gatewayClassExists($gateway)) {
261
                        $gatewayClassName = self::gatewayClassName($gateway);
262
                        $temporaryGateway = new $gatewayClassName();
263
                        if (self::asGoodAncestor($temporaryGateway)) {
264
                            return $temporaryGateway;
265
                        }
266
                    }
267
                }
268
            }
269
        }
270
271
        return null;
272
    }
273
}
274