Resource::getConsumptionState()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 1
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * Copyright 2015 Alexey Maslov <[email protected]>
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace alxmsl\Google\AndroidPublisher\Purchases\Products;
19
20
use alxmsl\Google\InitializationInterface;
21
22
/**
23
 * Class of in-app product purchase resource
24
 * @author alxmsl
25
 */
26
final class Resource implements InitializationInterface {
27
    /**
28
     * Consumption state constants
29
     */
30
    const STATE_YET_CONSUMED = 0,
31
          STATE_CONSUMED     = 1;
32
33
    /**
34
     * Order state constants
35
     */
36
    const ORDER_PURCHASED = 0,
37
          ORDER_CANCELLED = 1,
38
          ORDER_UNKNOWN   = -1;
39
40
    /**
41
     * @var int purchase consumption state
42
     */
43
    private $consumptionState = 0;
44
45
    /**
46
     * @var string developer-specified string that contains supplemental information about an order
47
     */
48
    private $developerPayload = '';
49
50
    /**
51
     * @var string represents an in-app purchase object in the android publisher service
52
     */
53
    private $kind = '';
54
55
    /**
56
     * @var int purchase state of the order
57
     */
58
    private $purchaseState = self::ORDER_UNKNOWN;
59
60
    /**
61
     * @var int time the product was purchased, milliseconds
62
     */
63
    private $purchaseTimeMillis = 0;
64
65
    /**
66
     * @return int purchase consumption state
67
     */
68 2
    public function getConsumptionState() {
69 2
        return $this->consumptionState;
70
    }
71
72
    /**
73
     * @return bool is purchase consumed
74
     */
75 2
    public function isConsumed() {
76 2
        return $this->getConsumptionState() == self::STATE_CONSUMED;
77
    }
78
79
    /**
80
     * @return string developer-specified string that contains supplemental information about an order
81
     */
82 2
    public function getDeveloperPayload() {
83 2
        return $this->developerPayload;
84
    }
85
86
    /**
87
     * @return string represents an in-app purchase object in the android publisher service
88
     */
89 2
    public function getKind() {
90 2
        return $this->kind;
91
    }
92
93
    /**
94
     * @return int purchase state of the order
95
     */
96 2
    public function getPurchaseState() {
97 2
        return $this->purchaseState;
98
    }
99
100
    /**
101
     * @return bool is product cancelled
102
     */
103 2
    public function isCancelled() {
104 2
        return $this->getPurchaseState() == self::ORDER_CANCELLED;
105
    }
106
107
    /**
108
     * @return bool is product purchased
109
     */
110 2
    public function isPurchased() {
111 2
        return $this->getPurchaseState() == self::ORDER_PURCHASED;
112
    }
113
114
    /**
115
     * @return int time the product was purchased, milliseconds
116
     */
117 2
    public function getPurchaseTimeMillis() {
118 2
        return $this->purchaseTimeMillis;
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124 1
    public static function initializeByString($string) {
125 1
        $Object   = json_decode($string);
126 1
        $Resource = new Resource();
127
128 1
        $Resource->consumptionState   = (int) $Object->consumptionState;
129 1
        $Resource->developerPayload   = (string) $Object->developerPayload;
130 1
        $Resource->kind               = (string) $Object->kind;
131 1
        $Resource->purchaseState      = (int) $Object->purchaseState;
132 1
        $Resource->purchaseTimeMillis = (int) $Object->purchaseTimeMillis;
133 1
        return $Resource;
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139 1
    public function __toString() {
140 1
        $consumptionState = 'unknown';
141 1
        switch ($this->getConsumptionState()) {
142 1
            case self::STATE_CONSUMED:
143 1
                $consumptionState = 'consumed';
144 1
                break;
145 1
            case self::STATE_YET_CONSUMED:
146 1
                $consumptionState = 'yet to be consumed';
147 1
                break;
148 1
        }
149 1
        $purchaseState = 'unknown';
150 1
        switch ($this->getPurchaseState()) {
151 1
            case self::ORDER_CANCELLED:
152 1
                $purchaseState = 'cancelled';
153 1
                break;
154 1
            case self::ORDER_PURCHASED:
155 1
                $purchaseState = 'purchased';
156 1
                break;
157 1
        }
158
159
        $format = <<<'EOD'
160
    consumptionState:   %s
161
    developerPayload:   %s
162
    kind:               %s
163
    purchaseState:      %s
164
    purchaseTimeMillis: %s
165 1
EOD;
166 1
        return sprintf($format
167 1
            , $consumptionState
168 1
            , $this->getDeveloperPayload()
169 1
            , $this->getKind()
170 1
            , $purchaseState
171 1
            , date('Y-m-d H:i:s', $this->getPurchaseTimeMillis() / 1000));
172
    }
173
}
174