Helper   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 131
ccs 41
cts 41
cp 1
rs 10
wmc 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
A camelCase() 0 9 1
A initialize() 0 7 4
A validateLuhn() 0 8 3
A convertToLowercase() 0 13 3
A getGatewayShortName() 0 11 3
A getGatewayClassName() 0 19 5
1
<?php
2
/**
3
 * Helper class
4
 */
5
6
namespace Omnipay\Common;
7
8
use InvalidArgumentException;
9
10
/**
11
 * Helper class
12
 *
13
 * This class defines various static utility functions that are in use
14
 * throughout the Omnipay system.
15
 */
16
class Helper
17
{
18
    /**
19
     * Convert a string to camelCase. Strings already in camelCase will not be harmed.
20
     *
21
     * @param  string  $str The input string
22
     * @return string camelCased output string
23
     */
24 75
    public static function camelCase($str)
25
    {
26 75
        $str = self::convertToLowercase($str);
27 75
        return preg_replace_callback(
28 75
            '/_([a-z])/',
29 75
            function ($match) {
30 6
                return strtoupper($match[1]);
31 75
            },
32 75
            $str
33
        );
34
    }
35
36
    /**
37
     * Convert strings with underscores to be all lowercase before camelCase is preformed.
38
     *
39
     * @param  string $str The input string
40
     * @return string The output string
41
     */
42 75
    protected static function convertToLowercase($str)
43
    {
44 75
        $explodedStr = explode('_', $str);
45 75
        $lowercasedStr = [];
46
47 75
        if (count($explodedStr) > 1) {
48 6
            foreach ($explodedStr as $value) {
49 6
                $lowercasedStr[] = strtolower($value);
50
            }
51 6
            $str = implode('_', $lowercasedStr);
52
        }
53
54 75
        return $str;
55
    }
56
57
    /**
58
     * Validate a card number according to the Luhn algorithm.
59
     *
60
     * @param  string  $number The card number to validate
61
     * @return boolean True if the supplied card number is valid
62
     */
63 21
    public static function validateLuhn($number)
64
    {
65 21
        $str = '';
66 21
        foreach (array_reverse(str_split($number)) as $i => $c) {
0 ignored issues
show
Bug introduced by
It seems like str_split($number) can also be of type true; however, parameter $array of array_reverse() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        foreach (array_reverse(/** @scrutinizer ignore-type */ str_split($number)) as $i => $c) {
Loading history...
67 21
            $str .= $i % 2 ? $c * 2 : $c;
68
        }
69
70 21
        return array_sum(str_split($str)) % 10 === 0;
0 ignored issues
show
Bug introduced by
It seems like str_split($str) can also be of type true; however, parameter $array of array_sum() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        return array_sum(/** @scrutinizer ignore-type */ str_split($str)) % 10 === 0;
Loading history...
71
    }
72
73
    /**
74
     * Initialize an object with a given array of parameters
75
     *
76
     * Parameters are automatically converted to camelCase. Any parameters which do
77
     * not match a setter on the target object are ignored.
78
     *
79
     * @param mixed $target     The object to set parameters on
80
     * @param array $parameters An array of parameters to set
81
     */
82 567
    public static function initialize($target, array $parameters = null)
83
    {
84 567
        if ($parameters) {
85 66
            foreach ($parameters as $key => $value) {
86 66
                $method = 'set'.ucfirst(static::camelCase($key));
87 66
                if (method_exists($target, $method)) {
88 66
                    $target->$method($value);
89
                }
90
            }
91
        }
92 567
    }
93
94
    /**
95
     * Resolve a gateway class to a short name.
96
     *
97
     * The short name can be used with GatewayFactory as an alias of the gateway class,
98
     * to create new instances of a gateway.
99
     */
100 18
    public static function getGatewayShortName($className)
101
    {
102 18
        if (0 === strpos($className, '\\')) {
103 9
            $className = substr($className, 1);
104
        }
105
106 18
        if (0 === strpos($className, 'Omnipay\\')) {
107 15
            return trim(str_replace('\\', '_', substr($className, 8, -7)), '_');
108
        }
109
110 3
        return '\\'.$className;
111
    }
112
113
    /**
114
     * Resolve a short gateway name to a full namespaced gateway class.
115
     *
116
     * Class names beginning with a namespace marker (\) are left intact.
117
     * Non-namespaced classes are expected to be in the \Omnipay namespace, e.g.:
118
     *
119
     *      \Custom\Gateway     => \Custom\Gateway
120
     *      \Custom_Gateway     => \Custom_Gateway
121
     *      Stripe              => \Omnipay\Stripe\Gateway
122
     *      PayPal\Express      => \Omnipay\PayPal\ExpressGateway
123
     *      PayPal_Express      => \Omnipay\PayPal\ExpressGateway
124
     *
125
     * @param  string  $shortName The short gateway name or the FQCN
126
     * @return string  The fully namespaced gateway class name
127
     */
128 24
    public static function getGatewayClassName($shortName)
129
    {
130 24
        // If the class starts with \ or Omnipay\, assume it's a FQCN
131 9
        if (0 === strpos($shortName, '\\') || 0 === strpos($shortName, 'Omnipay\\')) {
132
            return $shortName;
133
        }
134
135 15
        // Check if the class exists and implements the Gateway Interface, if so -> FCQN
136 15
        if (is_subclass_of($shortName, GatewayInterface::class, true)) {
137 6
            return $shortName;
138
        }
139
140 15
        // replace underscores with namespace marker, PSR-0 style
141
        $shortName = str_replace('_', '\\', $shortName);
142
        if (false === strpos($shortName, '\\')) {
143
            $shortName .= '\\';
144
        }
145
146
        return '\\Omnipay\\'.$shortName.'Gateway';
147
    }
148
}
149