Passed
Push — main ( 4dc4a3...97513a )
by Stas
14:40 queued 04:40
created

StoreSiteSelector::selectSite()   B

Complexity

Conditions 11
Paths 9

Size

Total Lines 75
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 44
c 1
b 0
f 0
nc 9
nop 3
dl 0
loc 75
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
namespace Huawei\IAP;
5
6
/**
7
 * Class StoreSiteSelector
8
 *
9
 * Allows select Store site which being used for verification.
10
 *
11
 * @package Huawei\IAP
12
 */
13
class StoreSiteSelector
14
{
15
    const CHINA = 'CN';
16
    const GERMANY = 'DE';
17
    const SINGAPORE = 'SG';
18
    const RUSSIA = 'RU';
19
20
    /** @var int
21
     * Account type. The options are as follows:
22
     * 1: AppTouch user account
23
     * Other values: HUAWEI ID
24
     *
25
     * @see https://developer.huawei.com/consumer/en/doc/development/HMS-References/iap-InAppPurchaseData#getAccountFlag
26
     */
27
    const ACCOUNT_FLAG = 1;
28
29
    protected $defaultOrderCountry = self::GERMANY;
30
    protected $defaultSubscriptionCountry = self::GERMANY;
31
32
    protected $appTouchOrderSiteMapping = [
33
        self::GERMANY => 'https://orders-at-dre.iap.dbankcloud.com',
34
    ];
35
36
    protected $appTouchSubscriptionSiteMapping = [
37
        self::GERMANY => 'https://subscr-at-dre.iap.dbankcloud.com',
38
    ];
39
40
    protected $orderSiteMapping = [
41
        self::CHINA => 'https://orders-drcn.iap.hicloud.com',
42
        self::GERMANY => 'https://orders-dre.iap.hicloud.com',
43
        self::SINGAPORE => 'https://orders-dra.iap.hicloud.com',
44
        self::RUSSIA => 'https://orders-drru.iap.hicloud.com',
45
    ];
46
47
    protected $subscriptionSiteMapping = [
48
        self::CHINA => 'https://subscr-drcn.iap.hicloud.com',
49
        self::GERMANY => 'https://subscr-dre.iap.hicloud.com',
50
        self::SINGAPORE => 'https://subscr-dra.iap.hicloud.com',
51
        self::RUSSIA => 'https://subscr-drru.iap.hicloud.com',
52
    ];
53
54
    /**
55
     * @param string $type $purchaseType ['order', 'subscription']
56
     * @param string|null $country - Desired country
57
     * @param int|null $accountFlag - If `1` app touch site will be used
58
     *
59
     * @see https://developer.huawei.com/consumer/en/doc/development/HMS-References/iap-api-specification-related-v4
60
     *
61
     * @throws \RuntimeException
62
     *
63
     * @return StoreSiteSelectorChoice
64
     */
65
    public function selectSite(
66
        string $type,
67
        ?string $country,
68
        ?int $accountFlag
69
    ): StoreSiteSelectorChoice
70
    {
71
        $appTouchSite = $accountFlag === self::ACCOUNT_FLAG;
72
73
        if ($type === Validator::TYPE_ORDER && $appTouchSite) {
74
            if (isset($this->appTouchOrderSiteMapping[$country])) {
75
                return new StoreSiteSelectorChoice(
76
                    $country,
0 ignored issues
show
Bug introduced by
It seems like $country can also be of type null; however, parameter $country of Huawei\IAP\StoreSiteSelectorChoice::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
                    /** @scrutinizer ignore-type */ $country,
Loading history...
77
                    $this->subscriptionSiteMapping[$country],
78
                    $appTouchSite
79
                );
80
            }
81
82
            return new StoreSiteSelectorChoice(
83
                self::GERMANY,
84
                $this->appTouchOrderSiteMapping[self::GERMANY],
85
                $appTouchSite
86
            );
87
        }
88
89
        if ($type === Validator::TYPE_ORDER) {
90
            if (isset($this->orderSiteMapping[$country])) {
91
                return new StoreSiteSelectorChoice(
92
                    $country,
93
                    $this->orderSiteMapping[$country],
94
                    $appTouchSite
95
                );
96
            }
97
98
            return new StoreSiteSelectorChoice(
99
                $this->defaultOrderCountry,
100
                $this->orderSiteMapping[$this->defaultOrderCountry],
101
                $appTouchSite
102
            );
103
        }
104
105
        if ($type === Validator::TYPE_SUBSCRIPTION && $appTouchSite) {
106
            if (isset($this->appTouchSubscriptionSiteMapping[$country])) {
107
                return new StoreSiteSelectorChoice(
108
                    $country,
109
                    $this->subscriptionSiteMapping[$country],
110
                    $appTouchSite
111
                );
112
            }
113
114
            return new StoreSiteSelectorChoice(
115
                self::GERMANY,
116
                $this->appTouchSubscriptionSiteMapping[self::GERMANY],
117
                $appTouchSite
118
            );
119
        }
120
121
        if ($type === Validator::TYPE_SUBSCRIPTION) {
122
            if (isset($this->subscriptionSiteMapping[$country])) {
123
                return new StoreSiteSelectorChoice(
124
                    $country,
125
                    $this->subscriptionSiteMapping[$country],
126
                    $appTouchSite
127
                );
128
            }
129
130
            return new StoreSiteSelectorChoice(
131
                $this->defaultSubscriptionCountry,
132
                $this->subscriptionSiteMapping[$this->defaultSubscriptionCountry],
133
                $appTouchSite
134
            );
135
        }
136
137
        throw new \RuntimeException(\sprintf(
138
            'Can not select site using (%s)',
139
            \json_encode(['type' => $type, 'country' => $country, 'accountFlag' => $accountFlag])
140
        ));
141
    }
142
143
}
144