Completed
Push — master ( a4f70a...5b7a0f )
by Andrii
05:16
created

Order::fromActions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\order;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\customer\CustomerInterface;
15
16
/**
17
 * @author Andrii Vasyliev <[email protected]>
18
 */
19
class Order implements OrderInterface
20
{
21
    /**
22
     * @var int|string|null
23
     */
24
    protected $id;
25
26
    /**
27
     * @var CustomerInterface
28
     */
29
    protected $customer;
30
31
    /**
32
     * @var ActionInterface[] array: actionKey => action
33
     */
34
    protected $actions = [];
35
36
    public function __construct($id, CustomerInterface $customer, array $actions = [])
37
    {
38
        $this->id = $id;
39
        $this->customer = $customer;
40
        $this->actions = $actions;
41
    }
42
43
    public static function fromAction(ActionInterface $action)
44
    {
45
        return new static(null, $action->getCustomer(), [$action]);
46
    }
47
48
    public static function fromActions(array $actions)
49
    {
50
        $action = reset($actions);
51
        if (! $action instanceof ActionInterface) {
52
            throw new \Exception('wrong actions given in ' . __METHOD__);
53
        }
54
55
        return new static(null, $action->getCustomer(), $actions);
56
    }
57
58
    public function getId()
59
    {
60
        return $this->id;
61
    }
62
63
    public function getCustomer()
64
    {
65
        return $this->customer;
66
    }
67
68
    /**
69
     * Returns actions.
70
     * @return ActionInterface[] array: actionKey => action
71
     */
72
    public function getActions()
73
    {
74
        return $this->actions;
75
    }
76
77
    public function jsonSerialize()
78
    {
79
        return get_object_vars($this);
80
    }
81
}
82