ChargeType::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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-2020, 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