Generalizer::isMoreGeneral()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

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