Completed
Push — master ( da296e...3ed755 )
by Dmitry
03:50
created

FormulaEngine::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
cc 2
eloc 3
nc 2
nop 0
crap 2.0625
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\formula;
12
13
use hiqdev\php\billing\charge\ChargeModifier;
14
use hiqdev\php\billing\charge\modifiers\Discount;
15
use Hoa\Ruler\Context;
16
use Hoa\Ruler\Ruler;
17
18
/**
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class FormulaEngine
22
{
23
    protected $ruler;
24
25
    protected $asserter;
26
27
    protected $context;
28
29
    protected $discount;
30
31 1
    public function __construct()
32
    {
33 1
        if (!class_exists(Context::class)) {
34
            throw new \Exception('to use formula engine install `hoa/ruler`');
35
        }
36 1
    }
37
38
    /**
39
     * @param string $formula
40
     * @return ChargeModifier
41
     */
42 1
    public function build(string $formula): ChargeModifier
43
    {
44 1
        return $this->getRuler()->assert($formula, $this->getContext());
45
    }
46
47 1
    public function getRuler()
48
    {
49 1
        if ($this->ruler === null) {
50 1
            $this->ruler = new Ruler();
51 1
            $this->ruler->setAsserter($this->getAsserter());
52
        }
53
54 1
        return $this->ruler;
55
    }
56
57 1
    public function getAsserter()
58
    {
59 1
        if ($this->asserter === null) {
60 1
            $this->asserter = new Asserter();
61
        }
62
63 1
        return $this->asserter;
64
    }
65
66 1
    public function getContext()
67
    {
68 1
        if ($this->context === null) {
69 1
            $this->context = $this->buildContext();
70
        }
71
72 1
        return $this->context;
73
    }
74
75 1
    protected function buildContext()
76
    {
77 1
        $context = new Context();
78 1
        $context['discount'] = $this->getDiscount();
79
80 1
        return $context;
81
    }
82
83 1
    public function getDiscount()
84
    {
85 1
        if ($this->discount === null) {
86 1
            $this->discount = new Discount();
87
        }
88
89 1
        return $this->discount;
90
    }
91
}
92