Completed
Push — master ( 854615...6f1a30 )
by Andrii
03:22
created

Plan   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 74
ccs 13
cts 17
cp 0.7647
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getPrices() 0 4 1
A calculateCharges() 0 12 3
A jsonSerialize() 0 4 1
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\plan;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\customer\CustomerInterface;
15
use hiqdev\php\billing\price\PriceInterface;
16
17
/**
18
 * Tariff Plan.
19
 *
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class Plan implements PlanInterface
23
{
24
    /**
25
     * @var int
26
     */
27
    protected $id;
28
29
    /**
30
     * @var string
31
     */
32
    protected $name;
33
34
    /**
35
     * @var Plan|null
36
     * XXX not sure to implement
37
     */
38
    protected $parent;
39
40
    /**
41
     * @var CustomerInterface
42
     */
43
    protected $seller;
44
45
    /**
46
     * @var PriceInterface[]
47
     */
48
    protected $prices = [];
49
50
    /**
51
     * @param PriceInterface[] $prices
52
     */
53 1
    public function __construct(
54
                            $id,
55
                            $name,
56
        CustomerInterface   $seller = null,
57
        array               $prices = []
58
    ) {
59 1
        $this->id = $id;
60 1
        $this->name = $name;
61 1
        $this->seller = $seller;
62 1
        $this->prices = $prices;
63 1
    }
64
65
    /**
66
     * @return PriceInterface[]
67
     */
68
    public function getPrices()
69
    {
70
        return $this->prices;
71
    }
72
73
    /**
74
     * Calculate charges for given action.
75
     * @param ActionInterface $action
76
     * @return Charge[]
77
     */
78 2
    public function calculateCharges(ActionInterface $action)
79
    {
80 2
        $charges = [];
81 2
        foreach ($this->prices as $price) {
82 2
            $charge = $action->calculateCharge($price);
83 2
            if ($charge !== null) {
84 2
                $charges[] = $charge;
85
            }
86
        }
87
88 2
        return $charges;
89
    }
90
91
    public function jsonSerialize()
92
    {
93
        return get_object_vars($this);
94
    }
95
}
96