Completed
Push — master ( 6c633d...7a6af2 )
by keika
10:40
created

Service::getTokenRequestArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4286
ccs 10
cts 10
cp 1
cc 2
eloc 9
nc 2
nop 0
crap 2
1
<?php
2
3
namespace keika299\ConohaAPI\Identity;
4
5
use keika299\ConohaAPI\Common\Service\AbstractService;
6
use keika299\ConohaAPI\Common\Network\Request;
7
8
/**
9
 * Class Service
10
 *
11
 * This class connect to ConoHa identity service.
12
 *
13
 * @package keika299\ConohaAPI\Identity
14
 */
15
class Service extends AbstractService
16
{
17
    /**
18
     * Get version information.
19
     *
20
     * See https://www.conoha.jp/docs/identity-get_version_list.html
21
     *
22
     * @return mixed
23
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
24
     */
25 1
    public function getVersionInfo()
26
    {
27 1
        $request = new Request();
28
        $request
29 1
            ->setMethod('GET')
30 1
            ->setBaseURI($this->baseURI)
31 1
            ->setURI('/')
32 1
            ->setAccept('application/json');
33
34 1
        $response = $request->exec();
35 1
        return $response->getJson();
36
    }
37
38
    /**
39
     * Get version detail.
40
     *
41
     * See https://www.conoha.jp/docs/identity-get_version_detail.html
42
     *
43
     * @return mixed
44
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
45
     */
46 1
    public function getVersionDetail()
47
    {
48 1
        $request = new Request();
49
        $request
50 1
            ->setMethod('GET')
51 1
            ->setBaseURI($this->baseURI)
52 1
            ->setURI('/v2.0')
53 1
            ->setAccept('application/json');
54
55 1
        $response = $request->exec();
56 1
        return $response->getJson();
57
    }
58
59
    /**
60
     * Request token.
61
     *
62
     * You don't have to run this function for use other service.
63
     * When create \keika299\ConohaAPI\Conoha object without token, try this and get token automatically.
64
     * See https://www.conoha.jp/docs/identity-post_tokens.html
65
     *
66
     * @return mixed
67
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
68
     */
69 4
    public function getToken()
70
    {
71 4
        $request = new Request();
72
        $request
73 4
            ->setMethod('POST')
74 4
            ->setBaseURI($this->baseURI)
75 4
            ->setURI('/v2.0/tokens')
76 4
            ->setAccept('application/json')
77 4
            ->setJson($this->getTokenRequestArray());
78 4
        $response = $request->exec();
79 4
        return $response->getJson();
80
    }
81
82
    /**
83
     * @return array
84
     */
85 4
    private function getTokenRequestArray()
86
    {
87
        $requestArray = array(
88
            'auth' => [
89
                'passwordCredentials' => [
90 4
                    'username' => $this->client->getUsername(),
91 4
                    'password' => $this->client->getUserPassword()
92 4
                ]
93 4
            ]
94 4
        );
95
96 4
        if ($this->client->getTenantID() !== null) {
97 4
            $requestArray["auth"]["tenantId"] = $this->client->getTenantID();
98 4
        }
99
100 4
        return $requestArray;
101
    }
102
}
103