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

Discount::fixed()   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-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