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

Generalizer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 60
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generalizeTarget() 0 17 1
A moreGeneral() 0 4 2
A lessGeneral() 0 4 2
B isMoreGeneral() 0 28 5
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