Completed
Push — master ( 9a2061...9f070b )
by Andrii
06:46
created

FormulaEngine::getDiscount()   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\ChargeModifier;
14
use hiqdev\php\billing\charge\modifiers\Discount;
15
use hiqdev\php\billing\charge\modifiers\Leasing;
16
use Hoa\Ruler\Context;
17
use Hoa\Ruler\Ruler;
18
19
/**
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class FormulaEngine
23
{
24
    protected $ruler;
25
26
    protected $asserter;
27
28
    protected $context;
29
30
    protected $discount;
31
32
    protected $leasing;
33
34 1
    public function __construct()
35
    {
36 1
        if (!class_exists(Context::class)) {
37
            throw new \Exception('to use formula engine install `hoa/ruler`');
38
        }
39 1
    }
40
41
    /**
42
     * @param string $formula
43
     * @return ChargeModifier
44
     */
45 1
    public function build(string $formula): ChargeModifier
46
    {
47 1
        return $this->getRuler()->assert($formula, $this->getContext());
48
    }
49
50 1
    public function getRuler()
51
    {
52 1
        if ($this->ruler === null) {
53 1
            $this->ruler = new Ruler();
54 1
            $this->ruler->setAsserter($this->getAsserter());
55
        }
56
57 1
        return $this->ruler;
58
    }
59
60 1
    public function getAsserter()
61
    {
62 1
        if ($this->asserter === null) {
63 1
            $this->asserter = new Asserter();
64
        }
65
66 1
        return $this->asserter;
67
    }
68
69 1
    public function getContext()
70
    {
71 1
        if ($this->context === null) {
72 1
            $this->context = $this->buildContext();
73
        }
74
75 1
        return $this->context;
76
    }
77
78 1
    protected function buildContext()
79
    {
80 1
        $context = new Context();
81 1
        $context['discount'] = $this->getDiscount();
82 1
        $context['leasing'] = $this->getLeasing();
83
84 1
        return $context;
85
    }
86
87 1
    public function getDiscount()
88
    {
89 1
        if ($this->discount === null) {
90 1
            $this->discount = new Discount();
91
        }
92
93 1
        return $this->discount;
94
    }
95
96 1
    public function getLeasing()
97
    {
98 1
        if ($this->leasing === null) {
99 1
            $this->leasing = new Leasing();
100
        }
101
102 1
        return $this->leasing;
103
    }
104
}
105