Completed
Push — master ( a1a818...828204 )
by Andrii
03:30
created

Generalizer::generalizeTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hiqdev\billing\hiapi\charge;
4
5
use DateTimeImmutable;
6
use hiqdev\php\billing\bill\Bill;
7
use hiqdev\php\billing\bill\BillInterface;
8
use hiqdev\php\billing\charge\ChargeInterface;
9
use hiqdev\php\billing\target\TargetInterface;
10
use hiqdev\php\units\QuantityInterface;
11
use Money\Money;
12
13
/**
14
 * @author Andrii Vasyliev <[email protected]>
15
 */
16
class Generalizer extends \hiqdev\php\billing\charge\Generalizer
17
{
18
    public function generalizeTarget(ChargeInterface $charge)
19
    {
20
        return $this->moreGeneral($charge->getAction()->getTarget(), $charge->getPrice()->getTarget());
21
22
        /* Sorry, older variants (to be removed later):
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
         * 1:
24
            if (in_array($charge->getTarget()->getType(), ['certificate', 'domain'], TRUE)) {
25
                $priceTarget = $charge->getPrice()->getTarget();
26
                if ($priceTarget->getId()) {
27
                    return $priceTarget;
28
                }
29
            }
30
            return parent::generalizeTarget($charge);
31
         * 2:
32
            return $priceTarget->getId() ? $priceTarget : new Target($charge->getSale()->getPlan()->getId(), 'plan');
33
         */
34
    }
35
36
    public function moreGeneral(TargetInterface $first, TargetInterface $other)
37
    {
38
        return $this->isMoreGeneral($first, $other) ? $first : $other;
39
    }
40
41
    public function lessGeneral(TargetInterface $first, TargetInterface $other)
42
    {
43
        return $this->isMoreGeneral($first, $other) ? $other : $first;
44
    }
45
46
    public function isMoreGeneral(TargetInterface $first, TargetInterface $other)
47
    {
48
        if (empty($first->getId()) && !empty($other->getId())) {
49
            return true;
50
        }
51
        if (empty($other->getId()) && !empty($first->getId())) {
52
            return false;
53
        }
54
55
        $i = 0;
56
        $order = [
57
            'domain'        => ++$i,
58
            'zone'          => ++$i,
59
            'certificate'   => ++$i,
60
            'type'          => ++$i,
61
            'part'          => ++$i,
62
            'server'        => ++$i,
63
            'device'        =>   $i,
64
            'tariff'        => ++$i,
65
            'ref'           => ++$i,
66
            ''              => ++$i,
67
        ];
68
69
        $lhs = $order[(string)$first->getType()];
70
        $rhs = $order[(string)$other->getType()];
71
72
        return $lhs > $rhs;
73
    }
74
75
}
76