Completed
Push — master ( e83455...d2af9f )
by Andrii
06:07 queued 04:16
created

ChargeType   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 24
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getValue() 0 3 1
A ensureValidValue() 0 7 2
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\addons;
12
13
use hiqdev\php\billing\charge\modifiers\AddonInterface;
14
15
/**
16
 * Charge type addon
17
 *
18
 * @author Dmytro Naumenko <[email protected]>
19
 */
20
class ChargeType implements AddonInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $value;
26
27
    public function __construct($value)
28
    {
29
        $this->value = $this->ensureValidValue($value);
30
    }
31
32
    public function getValue()
33
    {
34
        return $this->value;
35
    }
36
37
    private function ensureValidValue($value): string
38
    {
39
        if (!preg_match('/^[\w_]+$/', $value)) {
40
            throw new \InvalidArgumentException('Charge type is not valid');
41
        }
42
43
        return (string) $value;
44
    }
45
}
46