assign_payment_gateway()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
nc 1
nop 1
1
<?php
2
3
/**
4
 * if you want to implement rules around selecting
5
 * specific payment gateways for specific orders then
6
 * you need to extend this class and add the following to
7
 * mysite/_config/config.yml:
8
 * <code yml>
9
 *  Injector:
10
 *    EcommercePaymentSupportedMethodsProvider:
11
 *      class: MyCustom_EcommercePaymentSupportedMethodsProvider
12
 * </code>.
13
 *
14
 * in PHP you will have something like this:
15
 * <code php>
16
 * class MyCustom_EcommercePaymentSupportedMethodsProvider extends EcommercePaymentSupportedMethodsProvider {
17
 *  //....
18
 * }
19
 * </code>
20
 */
21
class EcommercePaymentSupportedMethodsProvider extends Object implements EcommercePaymentSupportedMethodsProviderInterface
22
{
23
    /**
24
     * this method returns an associative array of payment methods
25
     * available for the current order.
26
     *
27
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|integer|double|string|boolean?

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...
28
     */
29
    public function SupportedMethods($order = null)
30
    {
31
        $hideTestPaymentMethods = false;
32
        if (Director::isLive()) {
33
            $hideTestPaymentMethods = true;
34
        }
35
        $supportedMethods = EcommerceConfig::get('EcommercePayment', 'supported_methods');
36
        if (ArrayLib::is_associative($supportedMethods)) {
37
            if ($hideTestPaymentMethods) {
38
                if (count($supportedMethods)) {
39
                    foreach ($supportedMethods as $methodClass => $methodTitle) {
0 ignored issues
show
Bug introduced by
The expression $supportedMethods of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
40
                        if (is_subclass_of($methodClass, 'EcommercePayment_Test')) {
41
                            unset($supportedMethods[$methodClass]);
42
                        }
43
                    }
44
                }
45
            }
46
        } else {
47
            user_error('EcommercePayment::$supported_methods() requires an associative array. Right now the supported payments methods are: '.print_r($supportedMethods, 1), E_USER_NOTICE);
48
        }
49
        return $supportedMethods;
50
    }
51
52
    public static function assign_payment_gateway($gateway = "")
53
    {
54
        user_error("
55
            This function has not been implemented on this class.
56
            You can extend this class to allow for a supported method to be set.");
57
    }
58
    /**
59
     * returns the order to use....
60
     * You can provide one as a param,
61
     * which basically just checks that it is a real order.
62
     *
63
     * @param Order (optional) | Int
0 ignored issues
show
Documentation introduced by
Should the type for parameter $order not be optional|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...
64
     *
65
     * @return Order
0 ignored issues
show
Documentation introduced by
Should the return type not be Order|null?

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...
66
     */
67
    protected function orderToUse($order = null)
68
    {
69
        if ($order && $order instanceof Order) {
70
            return $order;
71
        }
72
        if (intval($order)) {
73
            return Order::get()->byID(intval($order));
74
        } else {
75
            return ShoppingCart::current_order();
76
        }
77
        user_error("Can't find an order to use");
0 ignored issues
show
Unused Code introduced by
user_error('Can\'t find an order to use'); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
78
    }
79
}
80