SteppedBillInterpreter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A interpret() 0 18 3
1
<?php
2
/**
3
 * Billing Boss
4
 *
5
 * @link      https://github.com/ranskills/billing-boss-php
6
 * @copyright Copyright (c) 2018 Ransford Ako Okpoti
7
 * @license   Refer to the LICENSE distributed with this library
8
 * @since     1.0
9
 */
10
namespace BillingBoss\Interpreter;
11
12
use BillingBoss\Expr;
13
use BillingBoss\BillContext;
14
use BillingBoss\AbstractBillInterpreter;
15
16
/**
17
 * Step bill interpreter.
18
 *
19
 * @package   BillingBoss
20
 * @link      https://github.com/ranskills/billing-boss-php
21
 * @copyright Copyright (c) 2018 Ransford Ako Okpoti
22
 * @license   Refer to the LICENSE distributed with this library
23
 * @since     1.0.0
24
 */
25
final class SteppedBillInterpreter extends AbstractBillInterpreter
26
{
27
    private const EXPRESSION =  Expr::POSITIVE_NUMBER .
28
                                Expr::COMMA .
29
                                Expr::POSITIVE_NUMBER .
30
                                '\+';
31
32
    public function __construct()
33
    {
34
        parent::__construct(sprintf('/^%s$/', self::EXPRESSION));
35
    }
36
37
    public function interpret(BillContext $context): float
38
    {
39
        if (!$this->isValid($context)) {
40
            return 0.0;
41
        }
42
        $bill = 0;
43
        \preg_match(sprintf('/^%s$/', self::EXPRESSION), $context->getStructure(), $matches);
44
45
        $amount = $context->getAmount();
46
        $billPerStep = $matches[1];
47
        $step = $matches[2];
48
49
        while ($amount > 0) {
50
            $bill += $billPerStep;
51
            $amount -= $step;
52
        }
53
54
        return $bill;
55
    }
56
}
57