BillingBoss   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 51
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getInterpreters() 0 13 2
A bill() 0 11 3
A removeInterpreter() 0 5 2
A addInterpreter() 0 3 1
1
<?php
2
3
namespace BillingBoss;
4
5
use BillingBoss\Interpreter\FlatRateBillInterpreter;
6
use BillingBoss\Interpreter\PercentageBillInterpreter;
7
use BillingBoss\Interpreter\CappedBillInterpreter;
8
use BillingBoss\Interpreter\ProgressiveBillInterpreter;
9
use BillingBoss\Interpreter\SteppedBillInterpreter;
10
11
/**
12
 * A billing interpreter aggregator
13
 *
14
 * @package   BillingBoss
15
 * @link      https://github.com/ranskills/billing-boss-php
16
 * @copyright Copyright (c) 2018 Ransford Ako Okpoti
17
 * @license   Refer to the LICENSE distributed with this library
18
 * @since     1.0.0
19
 */
20
final class BillingBoss
21
{
22
    /**
23
     * @var BillInterpreter[]
24
     */
25
    private static $interpreters = [];
26
27
    public static function getInterpreters()
28
    {
29
        if (!empty(self::$interpreters)) {
30
            return self::$interpreters;
31
        }
32
33
        self::addInterpreter(new CappedBillInterpreter());
34
        self::addInterpreter(new FlatRateBillInterpreter());
35
        self::addInterpreter(new PercentageBillInterpreter());
36
        self::addInterpreter(new ProgressiveBillInterpreter());
37
        self::addInterpreter(new SteppedBillInterpreter());
38
39
        return self::$interpreters;
40
    }
41
42
    public static function addInterpreter(BillInterpreter $interpreter)
43
    {
44
        self::$interpreters[get_class($interpreter)] = $interpreter;
45
    }
46
47
    public static function removeInterpreter(BillInterpreter $interpreter)
48
    {
49
        $key = get_class($interpreter);
50
        if (isset(self::$interpreters[$key])) {
51
            unset(self::$interpreters[$key]);
52
        }
53
    }
54
55
    /**
56
     * @param BillContext $context
57
     * @return float
58
     * @throws Exception\Exception
59
     */
60
    public static function bill(BillContext $context): float
61
    {
62
        $interpreters = self::getInterpreters();
63
64
        foreach ($interpreters as $interpreter) {
65
            if ($interpreter->isValid($context)) {
66
                return $interpreter->interpret($context);
67
            }
68
        }
69
70
        return 0.0;
71
    }
72
}
73