Passed
Push — main ( 352843...82cb43 )
by Stas
06:35
created

StoreSiteSelector::selectSite()   C

Complexity

Conditions 15
Paths 9

Size

Total Lines 75
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 15.0667

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 44
c 1
b 0
f 0
nc 9
nop 3
dl 0
loc 75
ccs 42
cts 45
cp 0.9333
crap 15.0667
rs 5.9166

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 14
    public function selectSite(
66
        string $type,
67
        ?string $country,
68
        ?int $accountFlag
69
    ): StoreSiteSelectorChoice
70
    {
71 14
        $appTouchSite = $accountFlag === self::ACCOUNT_FLAG;
72
73 14
        if ($type === Validator::TYPE_ORDER && $appTouchSite) {
74 2
            if ($country !== null && isset($this->appTouchOrderSiteMapping[$country])) {
75 1
                return new StoreSiteSelectorChoice(
76 1
                    $country,
77 1
                    $this->subscriptionSiteMapping[$country],
78 1
                    $appTouchSite
79
                );
80
            }
81
82 1
            return new StoreSiteSelectorChoice(
83 1
                self::GERMANY,
84 1
                $this->appTouchOrderSiteMapping[self::GERMANY],
85 1
                $appTouchSite
86
            );
87
        }
88
89 12
        if ($type === Validator::TYPE_ORDER) {
90 5
            if ($country !== null && isset($this->orderSiteMapping[$country])) {
91 4
                return new StoreSiteSelectorChoice(
92 4
                    $country,
93 4
                    $this->orderSiteMapping[$country],
94 4
                    $appTouchSite
95
                );
96
            }
97
98 1
            return new StoreSiteSelectorChoice(
99 1
                $this->defaultOrderCountry,
100 1
                $this->orderSiteMapping[$this->defaultOrderCountry],
101 1
                $appTouchSite
102
            );
103
        }
104
105 7
        if ($type === Validator::TYPE_SUBSCRIPTION && $appTouchSite) {
106 2
            if ($country !== null && isset($this->appTouchSubscriptionSiteMapping[$country])) {
107 1
                return new StoreSiteSelectorChoice(
108 1
                    $country,
109 1
                    $this->subscriptionSiteMapping[$country],
110 1
                    $appTouchSite
111
                );
112
            }
113
114 1
            return new StoreSiteSelectorChoice(
115 1
                self::GERMANY,
116 1
                $this->appTouchSubscriptionSiteMapping[self::GERMANY],
117 1
                $appTouchSite
118
            );
119
        }
120
121 5
        if ($type === Validator::TYPE_SUBSCRIPTION) {
122 5
            if ($country !== null && isset($this->subscriptionSiteMapping[$country])) {
123 4
                return new StoreSiteSelectorChoice(
124 4
                    $country,
125 4
                    $this->subscriptionSiteMapping[$country],
126 4
                    $appTouchSite
127
                );
128
            }
129
130 1
            return new StoreSiteSelectorChoice(
131 1
                $this->defaultSubscriptionCountry,
132 1
                $this->subscriptionSiteMapping[$this->defaultSubscriptionCountry],
133 1
                $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