Completed
Push — master ( 25a7f0...91ce38 )
by Dmitry
02:34
created

Generalizer::generalizeQuantity()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 14.1113

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 22
ccs 4
cts 14
cp 0.2857
rs 9.4888
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 14.1113
1
<?php
2
/**
3
 * API for Billing
4
 *
5
 * @link      https://github.com/hiqdev/billing-hiapi
6
 * @package   billing-hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\charge;
12
13
use hiqdev\billing\hiapi\type\TypeSemantics;
14
use hiqdev\php\billing\charge\ChargeInterface;
15
use hiqdev\php\billing\target\TargetInterface;
16
use hiqdev\php\billing\type\TypeInterface;
17
use hiqdev\php\units\Quantity;
18
use hiqdev\php\units\QuantityInterface;
19
20
/**
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class Generalizer extends \hiqdev\php\billing\charge\Generalizer
24
{
25
    /**
26
     * @var TypeSemantics
27
     */
28
    private $typeSemantics;
29
30 1
    public function __construct(TypeSemantics $typeSemantics)
31
    {
32 1
        $this->typeSemantics = $typeSemantics;
33 1
    }
34
35 1
    public function generalizeType(ChargeInterface $charge): TypeInterface
36
    {
37 1
        if ($charge->getParent() !== null) {
38
            $chargeType = $charge->getParent()->getPrice()->getType();
39
        } else {
40 1
            $chargeType = $charge->getPrice()->getType();
41
        }
42
43 1
        if ($this->typeSemantics->isMonthly($chargeType)) {
44
            return $this->typeSemantics->createMonthlyType();
45
        }
46
47 1
        return $chargeType;
48
    }
49
50 1
    public function generalizeQuantity(ChargeInterface $charge): QuantityInterface
51
    {
52 1
        $action = $charge->getAction();
53
54 1
        if ($action->getSale() !== null && $this->typeSemantics->isMonthly($action->getType())) {
55
            $actionMonth = $action->getTime()->modify('first day of this month 00:00');
56
            $saleMonth = $action->getSale()->getTime()->modify('first day of this month 00:00');
57
58
            if ($saleMonth > $actionMonth) {
59
                $amount = 0;
60
            } else if ($actionMonth > $saleMonth) {
61
                $amount = 1;
62
            } else {
63
                $saleDay = $action->getSale()->getTime()->format('d');
64
                $daysInMonth = $action->getSale()->getTime()->format('t');
65
                $amount = 1 - (($saleDay - 1) / $daysInMonth);
66
            }
67
68
            return Quantity::create('days', $amount);
69
        }
70
71 1
        return parent::generalizeQuantity($charge);
72
    }
73
74 1
    public function generalizeTarget(ChargeInterface $charge): TargetInterface
75
    {
76 1
        return $this->moreGeneral($charge->getAction()->getTarget(), $charge->getPrice()->getTarget());
77
78
        /* Sorry, to be removed later, older variants
79
         * 1:
80
            if (in_array($charge->getTarget()->getType(), ['certificate', 'domain'], TRUE)) {
81
                $priceTarget = $charge->getPrice()->getTarget();
82
                if ($priceTarget->getId()) {
83
                    return $priceTarget;
84
                }
85
            }
86
            return parent::generalizeTarget($charge);
87
         * 2:
88
            return $priceTarget->getId() ? $priceTarget : new Target($charge->getSale()->getPlan()->getId(), 'plan');
89
         */
90
    }
91
92 1
    public function moreGeneral(TargetInterface $first, TargetInterface $other)
93
    {
94 1
        return $this->isMoreGeneral($first, $other) || !$other->hasId() ? $first : $other;
95
    }
96
97
    public function lessGeneral(TargetInterface $first, TargetInterface $other)
98
    {
99
        return $this->isMoreGeneral($first, $other) || !$first->hasId() ? $other : $first;
100
    }
101
102 1
    public function isMoreGeneral(TargetInterface $first, TargetInterface $other)
103
    {
104 1
        $i = 0;
105
        $order = [
106 1
            'domain' => ++$i,
107 1
            'zone' => ++$i,
108 1
            'certificate' => ++$i,
109 1
            'type' => ++$i,
110 1
            'part' => ++$i,
111 1
            'server' => ++$i,
112 1
            'device' => $i,
113 1
            'tariff' => ++$i,
114 1
            'ref' => ++$i,
115 1
            '' => ++$i,
116
        ];
117
118 1
        $lhs = $order[(string)$first->getType()] ?? 0;
119 1
        $rhs = $order[(string)$other->getType()] ?? 0;
120
121 1
        return $lhs > $rhs;
122
    }
123
}
124