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

ShopTools::DBConn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
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