Passed
Push — master ( e3fcf4...82e94b )
by
unknown
02:37
created

updateCurrencyCodeForMoneyObect()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 10
nop 1
1
<?php
2
3
/**
4
 * Adds currency to invididual countries
5
 *
6
 */
7
8
class CountryPrice_EcommerceCurrency extends DataExtension
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...
9
{
10
    private static $has_many = array(
0 ignored issues
show
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
        "EcommerceCountries" => "EcommerceCountry"
12
    );
13
14
    public function updateCMSFields(FieldList $fields)
15
    {
16
        $fields->addFieldToTab(
17
            "Root.Countries",
18
            CheckboxSetField::create(
19
                'EcommerceCountries',
20
                'Countries',
21
                EcommerceCountry::get()
22
                    ->filter(array("EcommerceCurrencyID" => array(0, $this->owner->ID)))
23
                    ->sort(array("EcommerceCurrencyID" => "DESC", "Name" => "ASC"))
24
                    ->map()
25
            )
26
        );
27
        $fields->removeFieldFromTab("Root", "EcommerceCountries");
28
    }
29
30
31
    /**
32
     * @param strin $countryCode
33
     * @return EcommerceCurrency
34
     */
35
    public static function get_currency_for_country($countryCode)
36
    {
37
        $countryObject = CountryPrice_EcommerceCountry::get_real_country($countryCode);
38
        if ($countryObject) {
39
            $countryCode = $countryObject->Code;
40
        }
41
        $currencyPerCountry = CountryPrice_EcommerceCurrency::get_currency_per_country();
42
        $currencyDO = null;
43
        if ($countryCode) {
44
            $currencyCode = isset($currencyPerCountry[$countryCode]) ? $currencyPerCountry[$countryCode] : EcommerceCurrency::default_currency_code();
45
            $currencyDO = EcommerceCurrency::get_one_from_code($currencyCode);
46
        }
47
        if (! $currencyDO) {
48
            $currencyDO = EcommerceCurrency::create_new($currencyCode);
0 ignored issues
show
Bug introduced by
The variable $currencyCode does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
49
        }
50
        if (! $currencyDO) {
51
            $currencyDO = EcommerceCurrency::get_default();
0 ignored issues
show
Bug introduced by
The method get_default() does not seem to exist on object<EcommerceCurrency>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
        }
53
        return $currencyDO;
54
    }
55
56
    /**
57
     * finds the currency for each country
58
     * If no currency is found then the default currency is added.
59
     * returns something like
60
     *
61
     *     NZ => NZD
62
     *     AU => AUD
63
     *
64
     * @return array - list of countries and their currencies ...
65
     */
66
    public static function get_currency_per_country()
67
    {
68
        $cachekey = "EcommerceCurrencyCountryMatrix";
69
        $cache = SS_Cache::factory('CountryPrice_EcommerceCurrency');
70
        if (! ($serializedArray = $cache->load($cachekey))) {
71
            $countries = CountryPrice_EcommerceCountry::get_real_countries_list();
72
            $unserializedArray = array();
73
            $defaultCurrencyCode = EcommerceCurrency::default_currency_code();
74
            foreach ($countries as $countryObject) {
75
                $currencyCode = $defaultCurrencyCode;
76
                $currency = $countryObject->EcommerceCurrency();
77
                if ($currency && $currency->exists()) {
78
                    $currencyCode = $currency->Code;
79
                }
80
                $countryObject = CountryPrice_EcommerceCountry::get_real_country($countryObject);
81
                $unserializedArray[$countryObject->Code] = $currencyCode;
82
            }
83
            $cache->save(serialize($unserializedArray), $cachekey);
84
            return $unserializedArray;
85
        }
86
        return unserialize($serializedArray);
87
    }
88
89
    /**
90
     * list of currencies used on the site
91
     * @return Array
92
     */
93
    public static function get_currency_per_country_used_ones()
94
    {
95
        $resultArray = array();
96
        $functioningCountryObjects = EcommerceCountry::get()
97
            ->filter(array("DoNotAllowSales" => 0, 'AlwaysTheSameAsID' => 0))
98
            ->exclude(array("DistributorID" => 0));
99
        $countryCurrencies = CountryPrice_EcommerceCurrency::get_currency_per_country();
100
        if ($functioningCountryObjects->count()) {
101
            $countryCodes = $functioningCountryObjects->map("Code", "Code")->toArray();
102
            foreach ($countryCodes as $countryCode => $countryCodeAlso) {
103
                if (isset($countryCurrencies[$countryCode])) {
104
                    $resultArray[$countryCode] = $countryCurrencies[$countryCode];
105
                }
106
            }
107
        }
108
109
        return $resultArray;
110
    }
111
112
    private static $_money_object_currency = array();
113
    /***
114
     *
115
     * updates the Currency Code in the get_money_object_from_order_currency function
116
     * this is required for the googlebot read correctly the correct currencty as it does not have an order (see allowWrites in ShoppingCart class)
117
     * @param string $currencyCode
118
     * @return Array
119
     */
120
    public function updateCurrencyCodeForMoneyObect($currencyCode)
121
    {
122
        $countryCode = '';
123
        $countryObject = CountryPrice_EcommerceCountry::get_real_country();
124
        if ($countryObject) {
125
            $countryCode = $countryObject->Code;
126
        }
127
        if ($countryCode === '' || $countryCode === EcommerceConfig::get('EcommerceCountry', 'default_country_code')) {
128
            return null;
129
        }
130
        $key = $this->owner->ClassName.'____'.$countryCode;
131
        if (! isset(self::$_money_object_currency[$key])) {
132
            if(is_null($currencyCode)){
133
                $currency = CountryPrice_EcommerceCurrency::get_currency_for_country($countryCode);
134
                if ($currency) {
135
                    $currencyCode = strtoupper($currency->Code);
136
                }
137
                else {
138
                    return null;
139
                }
140
            }
141
            self::$_money_object_currency[$key] = $currencyCode;
142
        }
143
        return self::$_money_object_currency[$key];
144
    }
145
}
146