Completed
Push — master ( 104946...6683dc )
by Dmitry
02:26
created

Generalizer::generalizeTarget()   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.032

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.032
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 ($charge->getParent() !== null) {
39
            $chargeType = $charge->getParent()->getPrice()->getType();
40
        } else {
41 1
            $chargeType = $charge->getPrice()->getType();
42
        }
43
44 1
        if ($this->typeSemantics->isMonthly($chargeType)) {
45
            return $this->typeSemantics->createMonthlyType();
46
        }
47
48 1
        return $chargeType;
49
    }
50
51 1
    public function generalizeQuantity(ChargeInterface $charge): QuantityInterface
52
    {
53 1
        $action = $charge->getAction();
54
55 1
        if ($action->getSale() !== null && $this->typeSemantics->isMonthly($action->getType())) {
56
            $actionMonth = $action->getTime()->modify('first day of this month 00:00');
57
            $saleMonth = $action->getSale()->getTime()->modify('first day of this month 00:00');
58
59
            if ($saleMonth > $actionMonth) {
60
                $amount = 0;
61
            } else if ($actionMonth > $saleMonth) {
62
                $amount = 1;
63
            } else {
64
                $saleDay = $action->getSale()->getTime()->format('d');
65
                $daysInMonth = $action->getSale()->getTime()->format('t');
66
                $amount = 1 - (($saleDay - 1) / $daysInMonth);
67
            }
68
69
            return Quantity::create('days', $amount);
70
        }
71
72 1
        return parent::generalizeQuantity($charge);
73
    }
74
75 1
    public function generalizeTarget(ChargeInterface $charge): TargetInterface
76
    {
77 1
        $plan = $charge->getPrice()->getPlan();
78 1
        if ($plan instanceof GroupingPlan) {
79
            return $plan->convertToTarget();
80
        }
81
82 1
        return $this->moreGeneral($charge->getAction()->getTarget(), $charge->getPrice()->getTarget());
83
84
        /* Sorry, to be removed later, older variants
85
         * 1:
86
            if (in_array($charge->getTarget()->getType(), ['certificate', 'domain'], TRUE)) {
87
                $priceTarget = $charge->getPrice()->getTarget();
88
                if ($priceTarget->getId()) {
89
                    return $priceTarget;
90
                }
91
            }
92
            return parent::generalizeTarget($charge);
93
         * 2:
94
            return $priceTarget->getId() ? $priceTarget : new Target($charge->getSale()->getPlan()->getId(), 'plan');
95
         */
96
    }
97
98 1
    public function moreGeneral(TargetInterface $first, TargetInterface $other)
99
    {
100 1
        return $this->isMoreGeneral($first, $other) || !$other->hasId() ? $first : $other;
101
    }
102
103
    public function specializeTarget(TargetInterface $first, TargetInterface $other): TargetInterface
104
    {
105
        return $this->isMoreGeneral($first, $other) || !$first->hasId() ? $other : $first;
106
    }
107
108 1
    public function isMoreGeneral(TargetInterface $first, TargetInterface $other)
109
    {
110 1
        $i = 0;
111
        $order = [
112 1
            'domain' => ++$i,
113 1
            'zone' => ++$i,
114 1
            'certificate' => ++$i,
115 1
            'type' => ++$i,
116 1
            'part' => ++$i,
117 1
            'server' => ++$i,
118 1
            'device' => $i,
119 1
            'tariff' => ++$i,
120 1
            'ref' => ++$i,
121 1
            '' => ++$i,
122
        ];
123
124 1
        $lhs = $order[(string)$first->getType()] ?? 0;
125 1
        $rhs = $order[(string)$other->getType()] ?? 0;
126
127 1
        return $lhs > $rhs;
128
    }
129
}
130