Completed
Push — master ( d26128...b08c4e )
by Andrii
03:02
created

Order::fromAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 2
    public function __construct($id, CustomerInterface $customer, array $actions = [])
37
    {
38 2
        $this->id = $id;
39 2
        $this->customer = $customer;
40 2
        $this->actions = $actions;
41 2
    }
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 2
    public function getCustomer()
64
    {
65 2
        return $this->customer;
66
    }
67
68
    /**
69
     * Returns actions.
70
     * @return ActionInterface[] array: actionKey => action
71
     */
72 2
    public function getActions()
73
    {
74 2
        return $this->actions;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->actions returns the type hiqdev\php\billing\action\ActionInterface[] which is incompatible with the return type mandated by hiqdev\php\billing\order...Interface::getActions() of hiqdev\php\billing\order\ActionInterface[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
75
    }
76
77
    public function jsonSerialize()
78
    {
79
        return get_object_vars($this);
80
    }
81
}
82