Completed
Push — master ( 93750f...2ef50e )
by Andrii
02:27
created

SinglePrice::calculatePrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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;
12
13
use hiqdev\php\units\QuantityInterface;
14
use Money\Money;
15
16
/**
17
 * Single Price.
18
 *
19
 * - no charge for quantity less then prepaid
20
 * - same price for any quantity above prepaid
21
 *
22
 * @see PriceInterface
23
 *
24
 * @author Andrii Vasyliev <[email protected]>
25
 */
26
class SinglePrice extends AbstractPrice
27
{
28
    /**
29
     * @var Quantity prepaid quantity also implies Unit
30
     */
31
    protected $prepaid;
32
33
    /**
34
     * @var Money
35
     */
36
    protected $price;
37
38 3
    public function __construct(
39
                            $id,
40
        TargetInterface     $target,
41
        TypeInterface       $type,
42
        QuantityInterface   $prepaid,
43
        Money               $price
44
    ) {
45 3
        parent::__construct($id, $target, $type);
46 3
        $this->prepaid  = $prepaid;
0 ignored issues
show
Documentation Bug introduced by
It seems like $prepaid of type object<hiqdev\php\units\QuantityInterface> is incompatible with the declared type object<hiqdev\php\billing\Quantity> of property $prepaid.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47 3
        $this->price    = $price;
48 3
    }
49
50
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
    protected $properties = [
52
        'id'        => '',
53
        'target'    => [AbstractTarget::class, 'create'],
54
        'type'      => [Type::class, 'create'],
55
        'prepaid'   => [Quantity::class, 'create'],
56
        'price'     => [MoneyFactory::class, 'create'],
57
    ];
58
*/
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 3
    public function calculateUsage(QuantityInterface $quantity)
64
    {
65 3
        $usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid);
66
67 3
        return $usage->isPositive() ? $usage : null;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     * Same price for any usage.
73
     */
74 1
    public function calculatePrice(QuantityInterface $usage)
75
    {
76 1
        return $this->price;
77
    }
78
}
79