Skip to content

Commit 35e7020

Browse files
committed
Add Configuration class
1 parent 3d11dd6 commit 35e7020

File tree

5 files changed

+106
-36
lines changed

5 files changed

+106
-36
lines changed

examples/Looker.php renamed to examples/Configuration.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace App;
44

5-
class Looker extends \Alexkart\Looker\Looker {
6-
protected function storeAccessToken($accessToken): void {
5+
class Configuration extends \Alexkart\Looker\Configuration {
6+
public function storeAccessToken($accessToken): void {
77
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'looker_access_token.txt', $accessToken);
88
}
99

10-
protected function loadAccessToken(): string {
10+
public function loadAccessToken(): string {
1111
return (string)file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'looker_access_token.txt');
1212
}
1313
}

examples/test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
require_once(dirname(__DIR__) . '/vendor/autoload.php');
44

5-
$looker = new \Alexkart\Looker\Looker(
5+
$config = new \Alexkart\Looker\Configuration(
66
'https://looker-host:19999/api/3.1',
77
'client-id',
88
'client-secret',
99
'optional-access-token'
1010
);
11+
$looker = new \Alexkart\Looker\Looker($config);
1112

1213
$looks = $looker->lookApi->searchLooks(null, 'test');
1314
var_dump($looks);

examples/test1.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
require_once(dirname(__DIR__) . '/vendor/autoload.php');
44

5-
$looker = new \App\Looker(
5+
$config = new \App\Configuration(
66
'https://looker-host:19999/api/3.1',
77
'client-id',
88
'client-secret',
99
);
10+
$looker = new \Alexkart\Looker\Looker($config);
1011

1112
$looks = $looker->lookApi->searchLooks(null, 'test');
1213
var_dump($looks);

src/Configuration.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Alexkart\Looker;
4+
5+
class Configuration {
6+
private string $host;
7+
private string $clientId;
8+
private string $clientSecret;
9+
private string $accessToken;
10+
private bool $accessTokenRenewed = false;
11+
12+
public function __construct(string $host, string $clientId = '', string $clientSecret = '', string $accessToken = '') {
13+
$this->host = $host;
14+
$this->clientId = $clientId;
15+
$this->clientSecret = $clientSecret;
16+
$this->accessToken = $accessToken ?: $this->loadAccessToken();
17+
18+
$this->validate();
19+
}
20+
21+
private function validate() {
22+
if ($this->accessToken === '' && ($this->clientId === '' || $this->clientSecret === '')) {
23+
throw new \Exception('You must provide either a valid access token or API credentials.');
24+
}
25+
}
26+
27+
/**
28+
* @return string
29+
*/
30+
public function getHost(): string {
31+
return $this->host;
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function getClientId(): string {
38+
return $this->clientId;
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getClientSecret(): string {
45+
return $this->clientSecret;
46+
}
47+
48+
/**
49+
* @return string
50+
*/
51+
public function getAccessToken(): string {
52+
return $this->accessToken;
53+
}
54+
55+
/**
56+
* @param string $accessToken
57+
*/
58+
public function setAccessToken(string $accessToken): void {
59+
$this->accessToken = $accessToken;
60+
}
61+
62+
/**
63+
* @return bool
64+
*/
65+
public function isAccessTokenRenewed(): bool {
66+
return $this->accessTokenRenewed;
67+
}
68+
69+
/**
70+
* @param bool $accessTokenRenewed
71+
*/
72+
public function setAccessTokenRenewed(bool $accessTokenRenewed): void {
73+
$this->accessTokenRenewed = $accessTokenRenewed;
74+
}
75+
76+
public function storeAccessToken($accessToken): void {
77+
}
78+
79+
public function loadAccessToken(): string {
80+
return '';
81+
}
82+
}

src/Looker.php

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,48 +38,41 @@
3838
* @property Api\WorkspaceApi workspaceApi
3939
*/
4040
class Looker {
41-
private string $host;
42-
private string $clientId;
43-
private string $clientSecret;
44-
private string $accessToken;
4541
/**
4642
* @var void
4743
*/
4844
private Client $authenticatedClient;
4945
/**
5046
* @var void
5147
*/
52-
private $config;
53-
private bool $accessTokenRenewed = false;
48+
private $apiConfig;
49+
private \Alexkart\Looker\Configuration $config;
5450

55-
public function __construct(string $host, string $clientId, string $clientSecret, string $accessToken = '') {
56-
$this->host = $host;
57-
$this->clientId = $clientId;
58-
$this->clientSecret = $clientSecret;
59-
$this->accessToken = $accessToken ?: $this->loadAccessToken();
51+
public function __construct(\Alexkart\Looker\Configuration $config) {
52+
$this->config = $config;
6053
$this->login();
6154
}
6255

6356
public function __get(string $name) {
6457
$class = '\\Alexkart\\Looker\\' . ucfirst($name);
6558

66-
return new $class($this, $this->authenticatedClient, $this->config);
59+
return new $class($this, $this->authenticatedClient, $this->apiConfig);
6760
}
6861

6962
public function login(): void {
70-
$this->config = new Configuration();
71-
$this->config->setHost($this->host);
63+
$this->apiConfig = new Configuration();
64+
$this->apiConfig->setHost($this->config->getHost());
7265

73-
if ($this->accessToken === '') {
66+
if ($this->config->getAccessToken() === '') {
7467
$apiInstance = new ApiAuthApi(
7568
new Client(),
76-
$this->config
69+
$this->apiConfig
7770
);
7871

7972
try {
80-
$result = $apiInstance->login($this->clientId, $this->clientSecret);
81-
$this->accessToken = $result->getAccessToken();
82-
$this->accessTokenRenewed = true;
73+
$result = $apiInstance->login($this->config->getClientId(), $this->config->getClientSecret());
74+
$this->config->setAccessToken($result->getAccessToken());
75+
$this->config->setAccessTokenRenewed(true);
8376
} catch (Exception $e) {
8477
echo 'Exception when calling ApiAuthApi->login: ', $e->getMessage(), PHP_EOL;
8578
}
@@ -88,34 +81,27 @@ public function login(): void {
8881
$this->authenticatedClient = new Client([
8982
'verify' => false,
9083
'headers' => [
91-
'Authorization' => 'token ' . $this->accessToken,
84+
'Authorization' => 'token ' . $this->config->getAccessToken(),
9285
],
9386
]);
9487

95-
if ($this->accessTokenRenewed) {
96-
$this->storeAccessToken($this->accessToken);
88+
if ($this->config->isAccessTokenRenewed()) {
89+
$this->config->storeAccessToken($this->config->getAccessToken());
9790
}
9891
}
9992

10093
public function invalidateAccessToken(): void {
101-
$this->accessToken = '';
94+
$this->config->setAccessToken('');
10295
}
10396

10497
public function getAuthenticatedClient() {
10598
return $this->authenticatedClient;
10699
}
107100

108-
protected function storeAccessToken($accessToken): void {
109-
}
110-
111-
protected function loadAccessToken(): string {
112-
return '';
113-
}
114-
115101
/**
116102
* @return string
117103
*/
118104
public function getAccessToken(): string {
119-
return $this->accessToken;
105+
return $this->config->getAccessToken();
120106
}
121107
}

0 commit comments

Comments
 (0)