Client   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 26.09%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 58
rs 10
c 1
b 0
f 1
ccs 6
cts 23
cp 0.2609

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setPackage() 0 4 1
A getPackage() 0 3 1
B get() 0 23 5
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\InAppProducts;
19
20
use alxmsl\Google\AndroidPublisher\Exception\ErrorException;
21
use alxmsl\Google\AndroidPublisher\Exception\InvalidCredentialsException;
22
use alxmsl\Google\OAuth2\WebServerApplication;
23
use alxmsl\Network\Exception\HttpClientErrorCodeException;
24
use alxmsl\Network\Exception\HttpServerErrorCodeException;
25
use UnexpectedValueException;
26
use RuntimeException;
27
28
/**
29
 * Class for support GooglePlay InAppProducts API
30
 * @author alxmsl
31
 */
32
final class Client extends WebServerApplication {
33
    /**
34
     * GooglePlay Purchases Products API endpoint
35
     */
36
    const ENDPOINT_PURCHASES = 'https://www.googleapis.com/androidpublisher/v2';
37
38
    /**
39
     * @var string package name
40
     */
41
    private $package = '';
42
43
    /**
44
     * @param string $package package name
45
     * @return $this
46
     */
47
    public function setPackage($package) {
48
        $this->package = (string) $package;
49
        return $this;
50
    }
51
52
    /**
53
     * @return string package name
54
     */
55 1
    public function getPackage() {
56 1
        return $this->package;
57
    }
58
59
    /**
60
     * Returns information about the in-app product specified
61
     * @param string $productId product identifier
62
     * @return Resource product purchase resource instance
63
     * @throws ErrorException when API error acquired
64
     * @throws RuntimeException when type code for client is not supported now
65
     */
66 1
    public function get($productId) {
67 1
        $accessToken = $this->getAccessToken();
68 1
        if (!empty($accessToken)) {
69
            $Request = $this->getRequest(self::ENDPOINT_PURCHASES)
70
                ->addUrlField('applications', $this->getPackage())
71
                ->addUrlField('inappproducts', $productId)
72
                ->addGetField('access_token', $accessToken);
73
            try {
74
                return Resource::initializeByString($Request->send());
75
            } catch (HttpClientErrorCodeException $Ex) {
76
                switch ($Ex->getCode()) {
77
                    case 401:
78
                        throw InvalidCredentialsException::initializeByString($Ex->getMessage());
79
                    default:
80
                        throw ErrorException::initializeByString($Ex->getMessage());
81
                }
82
            } catch (HttpServerErrorCodeException $Ex) {
83
                throw ErrorException::initializeByString($Ex->getMessage());
84
            }
85
        } else {
86 1
            throw new UnexpectedValueException('access token is empty');
87
        }
88
    }
89
}
90