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

FormulaEngine::getAsserter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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