Passed
Pull Request — master (#74)
by
unknown
14:45
created

Formula   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 8
c 1
b 0
f 0
dl 0
loc 21
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A value() 0 3 1
A validate() 0 5 2
1
<?php declare(strict_types=1);
2
3
namespace hiqdev\php\billing\formula;
4
5
use hiqdev\php\billing\formula\FormulaEngine;
6
use hiqdev\php\billing\formula\FormulaEngineException;
7
8
class Formula
9
{
10
    public function __construct(private ?string $value, private readonly FormulaEngine $formulaEngine)
11
    {
12
        if (!empty($this->value)) {
13
            $this->value = $this->formulaEngine->normalize($this->value);
14
            $this->validate($this->value);
15
        }
16
    }
17
18
    private function validate(string $value): void
19
    {
20
        $error = $this->formulaEngine->validate($value);
21
        if ($error !== null) {
22
            throw new FormulaEngineException($error);
23
        }
24
    }
25
26
    public function value(): ?string
27
    {
28
        return $this->value;
29
    }
30
}