Completed
Push — master ( 7a6af2...ab0c16 )
by keika
08:07
created

AbstractService::getVersionInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4286
cc 1
eloc 8
nc 1
nop 0
crap 1
1
<?php
2
3
namespace keika299\ConohaAPI\Common\Service;
4
5
6
use keika299\ConohaAPI\Common\DataStore\Token;
7
use keika299\ConohaAPI\Common\Network\Request;
8
use keika299\ConohaAPI\Common\Network\Response;
9
use keika299\ConohaAPI\Conoha;
10
11
/**
12
 * Class AbstractService
13
 *
14
 * This class defines cloud service base.
15
 * This contain basic functions for services.
16
 *
17
 * @package keika299\ConohaAPI\Common\Service
18
 */
19
20
abstract class AbstractService
21
{
22
    protected $client;
23
    protected $token;
24
    protected $baseURI;
25
26
    /**
27
     * AbstractService constructor.
28
     * @param \keika299\ConohaAPI\Conoha $client
29
     * @param string $baseURI
30
     */
31 83
    public function __construct(Conoha $client, $baseURI)
32
    {
33 83
        $this->client = $client;
34 83
        $this->token = new Token($client);
35 83
        $this->baseURI = $baseURI;
36 83
    }
37
38
    /**
39
     * Refresh token and retry request.
40
     *
41
     * @param Request $request
42
     * @return Response
43
     */
44 1
    public function requestWithUpdatedToken(Request $request)
45
    {
46 1
        $this->token->refreshToken();
47 1
        $request->setToken($this->token->getToken());
48 1
        return $request->exec();
49
    }
50
51
    /**
52
     * Get version info.
53
     *
54
     * @return mixed
55
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
56
     */
57 4
    public function getVersionInfo()
58
    {
59 4
        $request = (new Request())
60 4
            ->setMethod('GET')
61 4
            ->setBaseURI($this->baseURI)
62 4
            ->setURI('/')
63 4
            ->setAccept('application/json');
64
65 4
        $response = $request->exec();
66 4
        return $response->getJson();
67
    }
68
}
69