From 49dabfd1ea93feffab414a2d58cdd94958c7f4db Mon Sep 17 00:00:00 2001
From: Christian Flothmann <christian.flothmann@xabbuh.de>
Date: Sat, 29 Mar 2014 22:49:11 +0100
Subject: [PATCH] reduce code duplicities

---
 src/AbstractApi.php        | 27 ++++++++++----------
 src/Api/Cloud.php          | 30 ++++++++++++-----------
 src/Signer/PandaSigner.php | 50 +++++++++++++++++---------------------
 3 files changed, 51 insertions(+), 56 deletions(-)

diff --git a/src/AbstractApi.php b/src/AbstractApi.php
index 2ce0743..0706ea0 100644
--- a/src/AbstractApi.php
+++ b/src/AbstractApi.php
@@ -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,
@@ -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']);
@@ -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)
+                );
+            }
+        }
+    }
 }
diff --git a/src/Api/Cloud.php b/src/Api/Cloud.php
index 19c6a95..3b8f07c 100644
--- a/src/Api/Cloud.php
+++ b/src/Api/Cloud.php
@@ -260,13 +260,10 @@ 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,
+        ));
     }
 
     /**
@@ -274,13 +271,10 @@ public function createEncodingWithProfileId(Video $video, $profileId)
      */
     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,
+        ));
     }
 
     /**
@@ -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);
+    }
 }
diff --git a/src/Signer/PandaSigner.php b/src/Signer/PandaSigner.php
index a9abb38..87b0e27 100644
--- a/src/Signer/PandaSigner.php
+++ b/src/Signer/PandaSigner.php
@@ -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);
@@ -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);
 
@@ -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;
+    }
 }