Completed
Push — master ( fdb40a...87cabe )
by Andrii
02:29
created

Plan::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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 3
    public function calculateCharges(ActionInterface $action)
79
    {
80 3
        $charges = [];
81 3
        foreach ($this->prices as $price) {
82 3
            $charge = $action->calculateCharge($price);
83 3
            if ($charge !== null) {
84 3
                $charges[] = $charge;
85 3
            }
86 3
        }
87
88 3
        return $charges;
89
    }
90
91
    public function jsonSerialize()
92
    {
93
        return get_object_vars($this);
94
    }
95
}
96