Last active
January 4, 2016 16:49
-
-
Save jamiehannaford/8650053 to your computer and use it in GitHub Desktop.
override service class for HP services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Guzzle\Http\ClientInterface; | |
use OpenCloud\Common\Service\CatalogService; | |
class HpService extends CatalogService | |
{ | |
public function __construct(ClientInterface $client, $type = null, $region = null, $urlType = null) | |
{ | |
$this->setClient($client); | |
$this->region = $region; | |
$this->type = $type; | |
$this->urlType = $urlType; | |
$this->setEndpoint($this->findHpEndpoint()); | |
$this->client->setBaseUrl($this->getBaseUrl()); | |
if ($this instanceof EventSubscriberInterface) { | |
$this->client->getEventDispatcher()->addSubscriber($this); | |
} | |
} | |
protected function findHpEndpoint() | |
{ | |
if (!$this->getClient()->getCatalog()) { | |
$this->getClient()->authenticate(); | |
} | |
$catalog = $this->getClient()->getCatalog(); | |
foreach ($catalog->getItems() as $service) { | |
if ($service->hasType($this->type)) { | |
return Endpoint::factory($service->getEndpointFromRegion($this->region)); | |
} | |
} | |
throw new \RuntimeException(sprintf( | |
'No endpoints for service type [%s], name [%s], region [%s] and urlType [%s]', | |
$this->type, | |
$this->name, | |
$this->region, | |
$this->urlType | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment