Completed
Push — 2.0 ( 1b72d1...7e42e6 )
by Roman
10s
created

ShopTools   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 57.58%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 11
c 3
b 0
f 1
lcom 0
cbo 5
dl 0
loc 65
ccs 19
cts 33
cp 0.5758
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A DBConn() 0 7 2
A price_for_display() 0 8 1
A get_current_locale() 0 12 3
B install_locale() 0 27 5
1
<?php
2
3
/**
4
 * Globally useful tools
5
 */
6
class ShopTools
7
{
8
    /**
9
     * Get the DB connection in a SS 3.1 and 3.2+ compatible way
10
     * @param string $name
11
     * @return SS_Database
12
     */
13 8
    public static function DBConn($name = 'default')
14
    {
15 8
        if (method_exists('DB', 'get_conn')) {
16 8
            return DB::get_conn($name);
17
        }
18
        return DB::getConn($name);
0 ignored issues
show
Deprecated Code introduced by
The method DB::getConn() has been deprecated with message: since version 4.0 Use DB::get_conn instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
19
    }
20
21
    public static function price_for_display($price)
22
    {
23
        $currency = ShopConfig::get_site_currency();
24
        $field = Money::create("Price");
25
        $field->setAmount($price);
26
        $field->setCurrency($currency);
27
        return $field;
28
    }
29
30 87
    public static function get_current_locale()
31
    {
32 87
        if (class_exists('Translatable')) {
33
            return Translatable::get_current_locale();
34
        }
35
36 87
        if (class_exists('Fluent')) {
37
            return Fluent::current_locale();
38
        }
39
40 87
        return i18n::get_locale();
41
    }
42
43 4
    public static function install_locale($locale)
44
    {
45
        // If the locale isn't given, silently fail (there might be carts that still have locale set to null)
46 4
        if (empty($locale)) {
47
            return;
48
        }
49
50 4
        if (class_exists('Translatable')) {
51
            Translatable::set_current_locale($locale);
52
        } else {
53 4
            if (class_exists('Fluent')) {
54
                Fluent::set_persist_locale($locale);
55
            }
56
        }
57
58
        // Do something like Fluent does to install the locale
59 4
        i18n::set_locale($locale);
60
61
        // LC_NUMERIC causes SQL errors for some locales (comma as decimal indicator) so skip
62 4
        foreach (array(LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_TIME) as $category) {
63 4
            setlocale($category, "{$locale}.UTF-8", $locale);
64 4
        }
65
        // Get date/time formats from Zend
66 4
        require_once 'Zend/Date.php';
67 4
        i18n::config()->date_format = Zend_Locale_Format::getDateFormat($locale);
68 4
        i18n::config()->time_format = Zend_Locale_Format::getTimeFormat($locale);
69 4
    }
70
}
71