Abstract.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Amazon
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Service_Abstract
  24. */
  25. require_once 'Zend/Service/Abstract.php';
  26. /**
  27. * Abstract Amazon class that handles the credentials for any of the Web Services that
  28. * Amazon offers
  29. *
  30. * @category Zend
  31. * @package Zend_Service
  32. * @subpackage Amazon
  33. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. abstract class Zend_Service_Amazon_Abstract extends Zend_Service_Abstract
  37. {
  38. /**
  39. * @var string Amazon Access Key
  40. */
  41. protected static $_defaultAccessKey = null;
  42. /**
  43. * @var string Amazon Secret Key
  44. */
  45. protected static $_defaultSecretKey = null;
  46. /**
  47. * @var string Amazon Region
  48. */
  49. protected static $_defaultRegion = null;
  50. /**
  51. * @var string Amazon Secret Key
  52. */
  53. protected $_secretKey;
  54. /**
  55. * @var string Amazon Access Key
  56. */
  57. protected $_accessKey;
  58. /**
  59. * @var string Amazon Region
  60. */
  61. protected $_region;
  62. /**
  63. * An array that contains all the valid Amazon Ec2 Regions.
  64. *
  65. * @var array
  66. */
  67. protected static $_validEc2Regions = array('eu-west-1', 'us-east-1');
  68. /**
  69. * Set the keys to use when accessing SQS.
  70. *
  71. * @param string $access_key Set the default Access Key
  72. * @param string $secret_key Set the default Secret Key
  73. * @return void
  74. */
  75. public static function setKeys($accessKey, $secretKey)
  76. {
  77. self::$_defaultAccessKey = $accessKey;
  78. self::$_defaultSecretKey = $secretKey;
  79. }
  80. /**
  81. * Set which region you are working in. It will append the
  82. * end point automaticly
  83. *
  84. * @param string $region
  85. */
  86. public static function setRegion($region)
  87. {
  88. if(in_array(strtolower($region), self::$_validEc2Regions, true)) {
  89. self::$_defaultRegion = $region;
  90. } else {
  91. require_once 'Zend/Service/Amazon/Exception.php';
  92. throw new Zend_Service_Amazon_Exception('Invalid Amazon Ec2 Region');
  93. }
  94. }
  95. /**
  96. * Create Amazon Sqs client.
  97. *
  98. * @param string $access_key Override the default Access Key
  99. * @param string $secret_key Override the default Secret Key
  100. * @param string $region Sets the AWS Region
  101. * @return void
  102. */
  103. public function __construct($accessKey=null, $secretKey=null, $region=null)
  104. {
  105. if(!$accessKey) {
  106. $accessKey = self::$_defaultAccessKey;
  107. }
  108. if(!$secretKey) {
  109. $secretKey = self::$_defaultSecretKey;
  110. }
  111. if(!$region) {
  112. $region = self::$_defaultRegion;
  113. } else {
  114. // make rue the region is valid
  115. if(!empty($region) && !in_array(strtolower($region), self::$_validEc2Regions, true)) {
  116. require_once 'Zend/Service/Amazon/Exception.php';
  117. throw new Zend_Service_Amazon_Exception('Invalid Amazon Ec2 Region');
  118. }
  119. }
  120. if(!$accessKey || !$secretKey) {
  121. require_once 'Zend/Service/Amazon/Exception.php';
  122. throw new Zend_Service_Amazon_Exception("AWS keys were not supplied");
  123. }
  124. $this->_accessKey = $accessKey;
  125. $this->_secretKey = $secretKey;
  126. $this->_region = $region;
  127. }
  128. /**
  129. * Method to fetch the AWS Region
  130. *
  131. * @return string
  132. */
  133. protected function _getRegion()
  134. {
  135. return (!empty($this->_region)) ? $this->_region . '.' : '';
  136. }
  137. /**
  138. * Method to fetch the Access Key
  139. *
  140. * @return string
  141. */
  142. protected function _getAccessKey()
  143. {
  144. return $this->_accessKey;
  145. }
  146. /**
  147. * Method to fetch the Secret AWS Key
  148. *
  149. * @return string
  150. */
  151. protected function _getSecretKey()
  152. {
  153. return $this->_secretKey;
  154. }
  155. }