Fee::withPercentage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
namespace Yabacon\Paystack;
3
4
class Fee
5
{
6
    const DEFAULT_PERCENTAGE = 0.015;
7
    const DEFAULT_ADDITIONAL_CHARGE = 10000;
8
    const DEFAULT_THRESHOLD = 250000;
9
    const DEFAULT_CAP = 200000;
10
11
    public static $default_percentage = Fee::DEFAULT_PERCENTAGE;
12
    public static $default_additional_charge = Fee::DEFAULT_ADDITIONAL_CHARGE;
13
    public static $default_threshold = Fee::DEFAULT_THRESHOLD;
14
    public static $default_cap = Fee::DEFAULT_CAP;
15
16
    private $percentage;
17
    private $additional_charge;
18
    private $threshold;
19
    private $cap;
20
21
    private $chargeDivider;
22
    private $crossover;
23
    private $flatlinePlusCharge;
24
    private $flatline;
25
26 4
    public function __construct()
27
    {
28 4
        $this->percentage = Fee::$default_percentage;
29 4
        $this->additional_charge = Fee::$default_additional_charge;
30 4
        $this->threshold = Fee::$default_threshold;
31 4
        $this->cap = Fee::$default_cap;
32 4
        $this->__setup();
33 4
    }
34
35 2
    public function withPercentage($percentage)
36
    {
37 2
        $this->percentage = $percentage;
38 2
        $this->__setup();
39 2
    }
40
41 1
    public static function resetDefaults()
42
    {
43 1
        Fee::$default_percentage = Fee::DEFAULT_PERCENTAGE;
44 1
        Fee::$default_additional_charge = Fee::DEFAULT_ADDITIONAL_CHARGE;
45 1
        Fee::$default_threshold = Fee::DEFAULT_THRESHOLD;
46 1
        Fee::$default_cap = Fee::DEFAULT_CAP;
47 1
    }
48
49 2
    public function withAdditionalCharge($additional_charge)
50
    {
51 2
        $this->additional_charge = $additional_charge;
52 2
        $this->__setup();
53 2
    }
54
55 2
    public function withThreshold($threshold)
56
    {
57 2
        $this->threshold = $threshold;
58 2
        $this->__setup();
59 2
    }
60
61 2
    public function withCap($cap)
62
    {
63 2
        $this->cap = $cap;
64 2
        $this->__setup();
65 2
    }
66
67 4
    private function __setup()
68
    {
69 4
        $this->chargeDivider = $this->__chargeDivider();
70 4
        $this->crossover = $this->__crossover();
71 4
        $this->flatlinePlusCharge = $this->__flatlinePlusCharge();
72 4
        $this->flatline = $this->__flatline();
73 4
    }
74
75 4
    private function __chargeDivider()
76
    {
77 4
        return 1 - $this->percentage;
78
    }
79
80 4
    private function __crossover()
81
    {
82 4
        return ($this->threshold * $this->chargeDivider) - $this->additional_charge;
83
    }
84
85 4
    private function __flatlinePlusCharge()
86
    {
87 4
        return ($this->cap - $this->additional_charge) / $this->percentage;
88
    }
89
90 4
    private function __flatline()
91
    {
92 4
        return $this->flatlinePlusCharge - $this->cap;
93
    }
94
95 3
    public function addFor($amountinkobo)
96
    {
97 3
        if ($amountinkobo > $this->flatline) {
98 1
            return intval(ceil($amountinkobo + $this->cap));
99 3
        } elseif ($amountinkobo > $this->crossover) {
100 3
            return intval(ceil(($amountinkobo + $this->additional_charge) / $this->chargeDivider));
101
        } else {
102 3
            return intval(ceil($amountinkobo / $this->chargeDivider));
103
        }
104
    }
105
106 2
    public function calculateFor($amountinkobo)
107
    {
108 2
        $fee = $this->percentage * $amountinkobo;
109 2
        if ($amountinkobo >= $this->threshold) {
110 2
            $fee += $this->additional_charge;
111 2
        }
112 2
        if ($fee > $this->cap) {
113 1
            $fee = $this->cap;
114 1
        }
115 2
        return intval(ceil($fee));
0 ignored issues
show
Bug introduced by
It seems like $fee can also be of type mixed; however, parameter $value of ceil() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        return intval(ceil(/** @scrutinizer ignore-type */ $fee));
Loading history...
116
    }
117
}
118