Completed
Push — master ( 888950...2eb122 )
by Andrii
05:25
created

Discount::ensureValidValue()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7.2944

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
ccs 9
cts 11
cp 0.8182
cc 7
eloc 11
nc 5
nop 1
crap 7.2944
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\Currency;
14
use Money\Money;
15
16
/**
17
 * General discount.
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class Discount extends Modifier
22
{
23 2
    public function fixed($value)
24
    {
25 2
        return new FixedDiscount($value, $this->addons);
26
    }
27
28 1
    public function grows($step, $start = null)
29
    {
30 1
        return new GrowingDiscount($step, $start, $this->addons);
31
    }
32
33 6
    public static function ensureValidValue($value)
34
    {
35 6
        if ($value instanceof Money) {
36 2
            return $value;
37
        }
38
39 5
        if (is_numeric($value)) {
40 3
            return (string) $value;
41
        }
42
43 3
        if (is_string($value) && preg_match('/^(\d{1,5}(\.\d+)?)%$/', $value, $ms)) {
44 2
            return $ms[1];
45
        }
46
47 1
        if (is_string($value) && preg_match('/^(\d{1,5}(\.\d+)?) ([A-Z]{3})$/', $value, $ms)) {
48 1
            return new Money($ms[1]*100, new Currency($ms[3]));
49
        }
50
51
        /// TODO: add special exception
52
        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...
53
        throw new \Exception('invalid discount value');
54
    }
55
}
56