Rounder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 67
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C round() 0 31 8
A getRoundingRules() 0 12 1
1
<?php
2
3
namespace Meanbee\MagentoRoyalmail\Model;
4
5
/**
6
 * Class Rounder
7
 */
8
class Rounder
9
{
10
    const NONE = 'none';
11
    const POUND = 'pound';
12
    const POUND_UP = 'pound-up';
13
    const POUND_DOWN = 'pound-down';
14
    const FIFTY = 'fifty';
15
    const FIFTY_UP = 'fifty_up';
16
    const FIFTY_DOWN = 'fifty_down';
17
18
    /**
19
     * Round price based on configuration settings
20
     *
21
     * @param string $roundingRule
22
     * @param float $number
23
     * @return float
24
     */
25 8
    public function round($roundingRule, $number)
26
    {
27 8
        $old = $number;
28
        switch ($roundingRule) {
29 8
            case static::POUND:
30 1
                $number = round($number);
31 1
                break;
32 7
            case static::POUND_UP:
33 1
                $number = ceil($number);
34 1
                break;
35 6
            case static::POUND_DOWN:
36 2
                $number = floor($number);
37 2
                break;
38 4
            case static::FIFTY:
39 1
                $number = round($number * 2) / 2;
40 1
                break;
41 3
            case static::FIFTY_UP:
42 1
                $number = ceil($number * 2) / 2;
43 1
                break;
44 2
            case static::FIFTY_DOWN:
45 1
                $number = floor($number * 2) / 2;
46 1
                break;
47
        }
48
49
        // Incase it rounds to 0
50 8
        if ($number == 0) {
51 1
            $number = ceil($old);
52 1
        }
53
54 8
        return $number;
55
    }
56
57
    /**
58
     * Get rounding rules available.
59
     *
60
     * @return array
61
     */
62 1
    public function getRoundingRules()
63
    {
64
        return [
65 1
            static::NONE => 'No rounding performed',
66 1
            static::POUND => 'Round to the nearest pound',
67 1
            static::POUND_UP => 'Round to the next whole pound',
68 1
            static::POUND_DOWN => 'Round to the previous whole pound',
69 1
            static::FIFTY => 'Round to the nearest 50p or whole pound',
70 1
            static::FIFTY_UP => 'Round to the next 50p or whole pound',
71 1
            static::FIFTY_DOWN => 'Round to the previous 50p or whole pound',
72 1
        ];
73
    }
74
}
75