Skip to content

Commit

Permalink
reduce code duplicities
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Mar 29, 2014
1 parent a329368 commit 49dabfd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 56 deletions.
27 changes: 13 additions & 14 deletions src/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,7 @@ protected function processAccountConfig(array $config)
}

foreach ($config['accounts'] as $name => $accountConfig) {
foreach (array('access_key', 'secret_key', 'api_host') as $option) {
if (!isset($accountConfig[$option])) {
throw new \InvalidArgumentException(
sprintf('Missing option %s for account %s', $option, $name)
);
}
}
$this->validateMandatoryOptions($accountConfig, 'account', $name, array('access_key', 'secret_key', 'api_host'));

$this->accountManager->registerAccount(
$name,
Expand All @@ -143,13 +137,7 @@ protected function processCloudConfig(array $config)
}

foreach ($config['clouds'] as $name => $cloudConfig) {
foreach (array('id', 'account') as $option) {
if (!isset($cloudConfig[$option])) {
throw new \InvalidArgumentException(
sprintf('Missing option %s for cloud %s', $option, $name)
);
}
}
$this->validateMandatoryOptions($cloudConfig, 'cloud', $name, array('id', 'account'));

try {
$account = $this->accountManager->getAccount($cloudConfig['account']);
Expand Down Expand Up @@ -327,4 +315,15 @@ public static function getCloudInstance($accessKey, $secretKey, $apiHost, $cloud

return $api->getCloud('default');
}

private function validateMandatoryOptions(array $config, $section, $name, array $options)
{
foreach ($options as $option) {
if (!isset($config[$option])) {
throw new \InvalidArgumentException(
sprintf('Missing option %s for %s %s', $option, $section, $name)
);
}
}
}
}
30 changes: 16 additions & 14 deletions src/Api/Cloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,27 +260,21 @@ public function createEncoding(Video $video, Profile $profile)
*/
public function createEncodingWithProfileId(Video $video, $profileId)
{
$response = $this->httpClient->post(
'/encodings.json',
array('video_id' => $video->getId(), 'profile_id' => $profileId)
);
$transformer = $this->transformers->getEncodingTransformer();

return $transformer->stringToEncoding($response);
return $this->doCreateEncoding(array(
'video_id' => $video->getId(),
'profile_id' => $profileId,
));
}

/**
* {@inheritDoc}
*/
public function createEncodingWithProfileName(Video $video, $profileName)
{
$response = $this->httpClient->post(
'/encodings.json',
array('video_id' => $video->getId(), 'profile_name' => $profileName,)
);
$transformer = $this->transformers->getEncodingTransformer();

return $transformer->stringToEncoding($response);
return $this->doCreateEncoding(array(
'video_id' => $video->getId(),
'profile_name' => $profileName,
));
}

/**
Expand Down Expand Up @@ -429,4 +423,12 @@ public function setNotifications(Notifications $notifications)

return $transformer->stringToNotifications($response);
}

private function doCreateEncoding(array $params)
{
$response = $this->httpClient->post('/encodings.json', $params);
$transformer = $this->transformers->getEncodingTransformer();

return $transformer->stringToEncoding($response);
}
}
50 changes: 22 additions & 28 deletions src/Signer/PandaSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,7 @@ public function getAccount()
*/
public function signParams($method, $path, array $params = array())
{
if (!isset($params['cloud_id'])) {
$params['cloud_id'] = $this->cloudId;
}

if (!isset($params['access_key'])) {
$params['access_key'] = $this->account->getAccessKey();
}

if (!isset($params['timestamp'])) {
$oldTz = date_default_timezone_get();
date_default_timezone_set('UTC');
$params['timestamp'] = date('c');
date_default_timezone_set($oldTz);
}
$params = $this->completeParams($params);

// generate the signature
$params['signature'] = $this->signature($method, $path, $params);
Expand All @@ -122,20 +109,7 @@ public function signParams($method, $path, array $params = array())
*/
public function signature($method, $path, array $params = array())
{
if (!isset($params['cloud_id'])) {
$params['cloud_id'] = $this->cloudId;
}

if (!isset($params['access_key'])) {
$params['access_key'] = $this->account->getAccessKey();
}

if (!isset($params['timestamp'])) {
$oldTz = date_default_timezone_get();
date_default_timezone_set('UTC');
$params['timestamp'] = date('c');
date_default_timezone_set($oldTz);
}
$params = $this->completeParams($params);

ksort($params);

Expand Down Expand Up @@ -176,4 +150,24 @@ public static function getInstance($cloudId, Account $account)

return $signer;
}

private function completeParams(array $params)
{
if (!isset($params['cloud_id'])) {
$params['cloud_id'] = $this->cloudId;
}

if (!isset($params['access_key'])) {
$params['access_key'] = $this->account->getAccessKey();
}

if (!isset($params['timestamp'])) {
$oldTz = date_default_timezone_get();
date_default_timezone_set('UTC');
$params['timestamp'] = date('c');
date_default_timezone_set($oldTz);
}

return $params;
}
}

0 comments on commit 49dabfd

Please sign in to comment.