Completed
Push — master ( 2a7c05...7c1d68 )
by Michael
02:50
created

currency   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

4 Methods

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