updateCurrencyCodeForMoneyObect()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
cc 6
nc 10
nop 1
1
<?php
2
3
/**
4
 * Adds currency to invididual countries
5
 *
6
 */
7
8
class CountryPrice_EcommerceCurrency extends DataExtension
9
{
10
    private static $has_many = array(
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)))
0 ignored issues
show
Bug introduced by
The property ID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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
113
    private static $_money_object_currency = array();
114
115
    /***
116
     *
117
     * updates the Currency Code in the get_money_object_from_order_currency function
118
     * this is required for the googlebot read correctly the correct currencty as it does not have an order (see allowWrites in ShoppingCart class)
119
     * @param string $currencyCode
120
     * @return Array
121
     */
122
    public function updateCurrencyCodeForMoneyObect($currencyCode)
123
    {
124
        $countryCode = '';
125
        $countryObject = CountryPrice_EcommerceCountry::get_real_country();
126
        if ($countryObject) {
127
            $countryCode = $countryObject->Code;
128
        }
129
        if ($countryCode === '') {
130
            return null;
131
        }
132
        $key = $this->owner->ClassName.'____'.$countryCode;
0 ignored issues
show
Bug introduced by
The property ClassName does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
133
        if (! isset(self::$_money_object_currency[$key])) {
134
            if (is_null($currencyCode)) {
135
                $currency = CountryPrice_EcommerceCurrency::get_currency_for_country($countryCode);
136
                if ($currency) {
137
                    $currencyCode = strtoupper($currency->Code);
138
                } else {
139
                    return null;
140
                }
141
            }
142
            self::$_money_object_currency[$key] = $currencyCode;
143
        }
144
        return self::$_money_object_currency[$key];
145
    }
146
}
147