Passed
Push — master ( df5caf...cd5737 )
by Michael
02:02
created

Currency::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php namespace XoopsModules\Adslight;
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
*/
12
13
/**
14
 * oledrion
15
 *
16
 * @copyright   {@link https://xoops.org/ XOOPS Project}
17
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
18
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
19
 */
20
21
22
/**
23
 * Gestion de la Currency
24
 */
25
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
27
class Currency
28
{
29
    protected $decimalsCount;
30
    protected $thousandsSep;
31
    protected $decimalSep;
32
    protected $moneyFull;
33
    protected $moneyShort;
34
    protected $currencyPosition;
35
36
    /**
37
     * Oledrion_Currency constructor.
38
     */
39
    public function __construct()
40
    {
41
        $moduleDirName = basename(__DIR__);
42
        $helper        = Helper::getHelper($moduleDirName);
43
44
        // Get the module's preferences
45
        $this->decimalsCount    = $helper->getConfig('decimals_count');
46
        $this->thousandsSep     = $helper->getConfig('thousands_sep');
47
        $this->decimalSep       = $helper->getConfig('decimal_sep');
48
        $this->moneyFull        = $helper->getConfig('money_full');
49
        $this->moneyShort       = $helper->getConfig('money_short');
50
        $this->currencyPosition = $helper->getConfig('currency_position');
51
        $this->thousandsSep     = str_replace('[space]', ' ', $this->thousandsSep);
52
        $this->decimalSep       = str_replace('[space]', ' ', $this->decimalSep);
53
    }
54
55
    /**
56
     * Access the only instance of this class
57
     *
58
     * @return object
59
     *
60
     * @static
61
     * @staticvar   object
62
     */
63
64
    public static function getInstance()
65
    {
66
        static $instance;
67
        if (null === $instance) {
68
            $instance = new static();
69
        }
70
71
        return $instance;
72
    }
73
74
    /**
75
     * Returns an amount according to the currency's preferences (defined in the module's options)
76
     *
77
     * @param  float|int $amount The amount to work on
78
     * @return string    The amount formated according to the currency
79
     */
80
    public function amountInCurrency($amount = 0)
81
    {
82
        return number_format($amount, $this->decimalsCount, $this->decimalSep, $this->thousandsSep);
83
    }
84
85
    /**
86
     * Format an amount for display according to module's preferences
87
     *
88
     * @param  float  $originalAmount The amount to format
89
     * @param  string $format         Format to use, 's' for Short and 'l' for Long
90
     * @return string The amount formated
91
     */
92
    public function amountForDisplay($originalAmount, $format = 's')
93
    {
94
        $amount = $this->amountInCurrency($originalAmount);
95
96
        $currencyLeft = $currencyRight = $currencyLeftShort = $currencyRightShort = '';
97
        if (1 == $this->currencyPosition) { // To the right
98
            $currencyRight      = '' . $this->moneyFull; // Long version
99
            $currencyRightShort = '' . $this->moneyShort; // Short version
100
        } else { // To the left
101
            $currencyLeft      = $this->moneyFull . ''; // Long version
102
            $currencyLeftShort = $this->moneyShort . ''; // Short version
103
        }
104
        if ('s' !== $format) {
105
            return $currencyLeft . $amount . $currencyRight;
106
        } else {
107
            return $currencyLeftShort . $amount . $currencyRightShort;
108
        }
109
    }
110
}
111