Service::getTokenRequestArray()   A
last analyzed

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 detail.
19
     *
20
     * See https://www.conoha.jp/docs/identity-get_version_detail.html
21
     *
22
     * @return mixed
23
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
24
     */
25 1
    public function getVersionDetail()
26
    {
27 1
        $request = new Request();
28
        $request
29 1
            ->setMethod('GET')
30 1
            ->setBaseURI($this->baseURI)
31 1
            ->setURI('/v2.0')
32 1
            ->setAccept('application/json');
33
34 1
        $response = $request->exec();
35 1
        return $response->getJson();
36
    }
37
38
    /**
39
     * Request token.
40
     *
41
     * You don't have to run this function for use other service.
42
     * When create \keika299\ConohaAPI\Conoha object without token, try this and get token automatically.
43
     * See https://www.conoha.jp/docs/identity-post_tokens.html
44
     *
45
     * @return mixed
46
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
47
     */
48 4
    public function getToken()
49
    {
50 4
        $request = new Request();
51
        $request
52 4
            ->setMethod('POST')
53 4
            ->setBaseURI($this->baseURI)
54 4
            ->setURI('/v2.0/tokens')
55 4
            ->setAccept('application/json')
56 4
            ->setJson($this->getTokenRequestArray());
57 4
        $response = $request->exec();
58 4
        return $response->getJson();
59
    }
60
61
    /**
62
     * @return array
63
     */
64 4
    private function getTokenRequestArray()
65
    {
66
        $requestArray = array(
67
            'auth' => [
68
                'passwordCredentials' => [
69 4
                    'username' => $this->client->getUsername(),
70 4
                    'password' => $this->client->getUserPassword()
71 4
                ]
72 4
            ]
73 4
        );
74
75 4
        if ($this->client->getTenantID() !== null) {
76 4
            $requestArray["auth"]["tenantId"] = $this->client->getTenantID();
77 4
        }
78
79 4
        return $requestArray;
80
    }
81
}
82