Passed
Push — master ( 3e9959...0d684b )
by Dariusz
02:37
created

Payment   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 139
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getId() 0 4 1
A getName() 0 4 1
A getDescription() 0 4 1
A setFixed() 0 4 1
A setPercentage() 0 4 1
A getFee() 0 8 2
A setPriceFormat() 0 4 1
A calculateFee() 0 8 2
1
<?php
2
3
namespace Plane\Shop;
4
5
use Plane\Shop\PriceFormat\PriceFormatInterface;
6
7
/**
8
 * Payment class
9
 *
10
 * @author Dariusz Korsak <[email protected]>
11
 * @package Plane\Shop
12
 */
13
class Payment implements PaymentInterface
14
{
15
    /**
16
     * Fee is a fixed price
17
     */
18
    const FEE_FIXED = 'fixed';
19
    
20
    /**
21
     * Fee is calculated as a percentage of total price
22
     */
23
    const FEE_PERCENTAGE = 'percentage';
24
    
25
    /**
26
     * Payment id
27
     * @var int
28
     */
29
    private $id;
30
    
31
    /**
32
     * Payment name
33
     * @var string
34
     */
35
    private $name;
36
    
37
    /**
38
     * Payment description
39
     * @var string
40
     */
41
    private $description;
42
    
43
    /**
44
     * Payment fee
45
     * @var int|float
46
     */
47
    private $fee;
48
    
49
    /**
50
     * Payment fee type (fee_fixed|fee_percentage)
51
     * @var string
52
     */
53
    private $feeType = self::FEE_FIXED;
54
    
55
    /**
56
     * Price format object
57
     * @var \Plane\Shop\PriceFormat\PriceFormatInterface
58
     */
59
    private $priceFormat;
60
    
61
    /**
62
     * Constructor
63
     * @param array $data
64
     */
65 3
    public function __construct(array $data)
66
    {
67 3
        foreach ($data as $k => $v) {
68 3
            $this->$k = $v;
69 3
        }
70 3
    }
71
    
72
    /**
73
     * Return id
74
     * @return int
75
     */
76 1
    public function getId()
77
    {
78 1
        return $this->id;
79
    }
80
    
81
    /**
82
     * Return name
83
     * @return string
84
     */
85 1
    public function getName()
86
    {
87 1
        return $this->name;
88
    }
89
    
90
    /**
91
     * Return description
92
     * @return string
93
     */
94 1
    public function getDescription()
95
    {
96 1
        return $this->description;
97
    }
98
    
99
    /**
100
     * Set fee as fixed price
101
     */
102 1
    public function setFixed()
103
    {
104 1
        $this->feeType = self::FEE_FIXED;
105 1
    }
106
    
107
    /**
108
     * Set fee as percentage of total price
109
     */
110 1
    public function setPercentage()
111
    {
112 1
        $this->feeType = self::FEE_PERCENTAGE;
113 1
    }
114
    
115
    /**
116
     * Return fee
117
     * @param float $totalPrice
118
     * @return float
119
     */
120 3
    public function getFee($totalPrice)
121
    {
122 3
        if (!is_null($this->priceFormat)) {
123 1
            return $this->priceFormat->formatPrice($this->calculateFee($totalPrice));
124
        }
125
        
126 2
        return $this->calculateFee($totalPrice);
127
    }
128
    
129
    /**
130
     * Set price format object
131
     * @param \Plane\Shop\PriceFormat\PriceFormatInterface $priceFormat
132
     */
133 1
    public function setPriceFormat(PriceFormatInterface $priceFormat)
134
    {
135 1
        $this->priceFormat = $priceFormat;
136 1
    }
137
    
138
    /**
139
     * Calculate fee based on feeType
140
     * @param double $totalPrice
141
     * @return float
142
     */
143 3
    protected function calculateFee($totalPrice)
144
    {
145 3
        if ($this->feeType == self::FEE_PERCENTAGE) {
146 1
            return (float) $totalPrice * $this->fee;
147
        }
148
        
149 2
        return (float) $this->fee;
150
    }
151
}
152