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

StoreSiteSelector   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
c 1
b 0
f 0
dl 0
loc 127
ccs 42
cts 45
cp 0.9333
rs 10
wmc 15

1 Method

Rating   Name   Duplication   Size   Complexity  
C selectSite() 0 75 15
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