Passed
Push — master ( 6ae125...ae3d7a )
by Dmitry
02:46
created

Generalizer::moreGeneral()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 3
nc 4
nop 2
crap 3
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
18
/**
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class Generalizer extends \hiqdev\php\billing\charge\Generalizer
22
{
23
    /**
24
     * @var TypeSemantics
25
     */
26
    private $typeSemantics;
27
28 1
    public function __construct(TypeSemantics $typeSemantics)
29
    {
30 1
        $this->typeSemantics = $typeSemantics;
31 1
    }
32
33 1
    public function generalizeType(ChargeInterface $charge): TypeInterface
34
    {
35 1
        $chargeType = $charge->getPrice()->getType();
36
37 1
        if ($this->typeSemantics->isMonthly($chargeType)) {
38
            return $this->typeSemantics->createMonthlyType();
39
        }
40
41 1
        return $charge->getPrice()->getType();
42
    }
43
44 1
    public function generalizeTarget(ChargeInterface $charge): TargetInterface
45
    {
46 1
        return $this->moreGeneral($charge->getAction()->getTarget(), $charge->getPrice()->getTarget());
47
48
        /* Sorry, to be removed later, older variants
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...
49
         * 1:
50
            if (in_array($charge->getTarget()->getType(), ['certificate', 'domain'], TRUE)) {
51
                $priceTarget = $charge->getPrice()->getTarget();
52
                if ($priceTarget->getId()) {
53
                    return $priceTarget;
54
                }
55
            }
56
            return parent::generalizeTarget($charge);
57
         * 2:
58
            return $priceTarget->getId() ? $priceTarget : new Target($charge->getSale()->getPlan()->getId(), 'plan');
59
         */
60
    }
61
62 1
    public function moreGeneral(TargetInterface $first, TargetInterface $other)
63
    {
64 1
        return $this->isMoreGeneral($first, $other) || !$other->hasId() ? $first : $other;
65
    }
66
67
    public function lessGeneral(TargetInterface $first, TargetInterface $other)
68
    {
69
        return $this->isMoreGeneral($first, $other) || !$first->hasId() ? $other : $first;
70
    }
71
72 1
    public function isMoreGeneral(TargetInterface $first, TargetInterface $other)
73
    {
74 1
        $i = 0;
75
        $order = [
76 1
            'domain'        => ++$i,
77 1
            'zone'          => ++$i,
78 1
            'certificate'   => ++$i,
79 1
            'type'          => ++$i,
80 1
            'part'          => ++$i,
81 1
            'server'        => ++$i,
82 1
            'device'        =>   $i,
83 1
            'tariff'        => ++$i,
84 1
            'ref'           => ++$i,
85 1
            ''              => ++$i,
86
        ];
87
88 1
        $lhs = $order[(string) $first->getType()] ?? 0;
89 1
        $rhs = $order[(string) $other->getType()] ?? 0;
90
91 1
        return $lhs > $rhs;
92
    }
93
}
94