@@ -10,11 +10,11 @@ discard block |
||
10 | 10 | class SessionHandler |
11 | 11 | { |
12 | 12 | public static ?Client $client=null; |
13 | - public static function getSession(VmwareApiClient $client){ |
|
13 | + public static function getSession(VmwareApiClient $client) { |
|
14 | 14 | self::validateCredentials($client); |
15 | 15 | return self::isSessionExist($client->credential) ? $client->credential['Vmware-Api-Session-Id'] : self::authRequest($client); |
16 | 16 | } |
17 | - public static function authRequest(VmwareApiClient $client){ |
|
17 | + public static function authRequest(VmwareApiClient $client) { |
|
18 | 18 | $HttpClient=isset(static::$client) ? self::$client : new Client( |
19 | 19 | [ |
20 | 20 | "verify"=>$client->ssl, |
@@ -25,23 +25,23 @@ discard block |
||
25 | 25 | ] |
26 | 26 | ] |
27 | 27 | ); |
28 | - $response=$HttpClient->request('GET',$client->getAuthUrl(),[ |
|
28 | + $response=$HttpClient->request('GET', $client->getAuthUrl(), [ |
|
29 | 29 | "query"=>[ |
30 | 30 | "~method"=>"post" |
31 | 31 | ] |
32 | 32 | ]); |
33 | 33 | return json_decode($response->getBody())->value; |
34 | 34 | } |
35 | - public static function isSessionExist($credential){ |
|
36 | - return array_key_exists("Vmware-Api-Session-Id",$credential); |
|
35 | + public static function isSessionExist($credential) { |
|
36 | + return array_key_exists("Vmware-Api-Session-Id", $credential); |
|
37 | 37 | } |
38 | 38 | public static function validateCredentials(VmwareApiClient $client) |
39 | 39 | { |
40 | - if((array_key_exists("username",$client->credential) && array_key_exists("password",$client->credential)) or array_key_exists("Vmware-Api-Session-Id",$client->credential)) |
|
40 | + if ((array_key_exists("username", $client->credential) && array_key_exists("password", $client->credential)) or array_key_exists("Vmware-Api-Session-Id", $client->credential)) |
|
41 | 41 | return true; |
42 | 42 | throw new CredentialException("required parameter you should send session-id or username-password for auth api "); |
43 | 43 | } |
44 | - public static function convertCredentials($credentials){ |
|
44 | + public static function convertCredentials($credentials) { |
|
45 | 45 | return base64_encode($credentials['username'].":".$credentials['password']); |
46 | 46 | } |
47 | 47 |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | use FNDEV\vShpare\Auth\SessionHandler; |
4 | 4 | use FNDEV\vShpare\Client\GuzzleClient; |
5 | 5 | use \GuzzleHttp\Client; |
6 | - class VmwareApiClient{ |
|
6 | + class VmwareApiClient { |
|
7 | 7 | public $host; |
8 | 8 | public $credential; |
9 | 9 | public $HttpClient; |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | public $authUrl; |
13 | 13 | public $protocol; |
14 | 14 | public $port; |
15 | - public function __construct($host,array $credential,$port=443,$ssl=false,$protocol="https",$baseurl="/rest/vcenter",$authurl="/rest/com/vmware/cis/session",?Client $client=null) |
|
15 | + public function __construct($host, array $credential, $port=443, $ssl=false, $protocol="https", $baseurl="/rest/vcenter", $authurl="/rest/com/vmware/cis/session", ?Client $client=null) |
|
16 | 16 | { |
17 | 17 | $this->host=$host; |
18 | 18 | $this->port=$port; |
@@ -21,30 +21,30 @@ discard block |
||
21 | 21 | $this->baseUrl=$baseurl; |
22 | 22 | $this->authUrl=$authurl; |
23 | 23 | $this->protocol=$protocol; |
24 | - $this->HttpClient=$client??new GuzzleClient($this); |
|
24 | + $this->HttpClient=$client ?? new GuzzleClient($this); |
|
25 | 25 | } |
26 | - public function vm(){ |
|
26 | + public function vm() { |
|
27 | 27 | return new Api\VM\VM($this->HttpClient); |
28 | 28 | } |
29 | - public function getHttpClient(){ |
|
29 | + public function getHttpClient() { |
|
30 | 30 | return $this->HttpClient; |
31 | 31 | } |
32 | - public function setHttpClient(Client $client){ |
|
32 | + public function setHttpClient(Client $client) { |
|
33 | 33 | $this->HttpClient=$client; |
34 | 34 | } |
35 | - public function getSessionId(){ |
|
35 | + public function getSessionId() { |
|
36 | 36 | return SessionHandler::getSession($this); |
37 | 37 | } |
38 | - public function getBaseUrl(){ |
|
39 | - return $this->addScheme($this->host.":".$this->port."/".trim($this->baseUrl,"/")."/",$this->protocol); |
|
38 | + public function getBaseUrl() { |
|
39 | + return $this->addScheme($this->host.":".$this->port."/".trim($this->baseUrl, "/")."/", $this->protocol); |
|
40 | 40 | } |
41 | - public function getAuthUrl(){ |
|
42 | - return $this->addScheme($this->host.":".$this->port."/".trim($this->authUrl,"/"),$this->protocol); |
|
41 | + public function getAuthUrl() { |
|
42 | + return $this->addScheme($this->host.":".$this->port."/".trim($this->authUrl, "/"), $this->protocol); |
|
43 | 43 | } |
44 | - function addScheme($url, $scheme = 'http') |
|
44 | + function addScheme($url, $scheme='http') |
|
45 | 45 | { |
46 | - return parse_url($url, PHP_URL_SCHEME) === null ? |
|
47 | - $scheme."://" . $url : $url; |
|
46 | + return parse_url($url, PHP_URL_SCHEME)===null ? |
|
47 | + $scheme."://".$url : $url; |
|
48 | 48 | } |
49 | 49 | } |
50 | 50 |