Completed
Push — master ( f0c93a...888950 )
by Andrii
05:26
created

Discount::ensureValidValue()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.2742

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
ccs 7
cts 9
cp 0.7778
cc 5
eloc 9
nc 4
nop 1
crap 5.2742
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\charge\modifiers;
12
13
use Money\Money;
14
15
/**
16
 * General discount.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class Discount extends Modifier
21
{
22 2
    public function fixed($value)
23
    {
24 2
        return new FixedDiscount($value, $this->addons);
25
    }
26
27 1
    public function grows($step, $start = null)
28
    {
29 1
        return new GrowingDiscount($step, $start, $this->addons);
30
    }
31
32 6
    public static function ensureValidValue($value)
33
    {
34 6
        if ($value instanceof Money) {
35 2
            return $value;
36
        }
37
38 4
        if (is_numeric($value)) {
39 3
            return (string) $value;
40
        }
41
42 2
        if (is_string($value) && preg_match('/^(\d{1,5}(\.\d+)?)%$/', $value, $matches)) {
43 2
            return $matches[1];
44
        }
45
46
        /// TODO: add special exception
47
        var_dump($value);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($value); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
48
        throw new \Exception('invalid discount value');
49
    }
50
}
51