Abstract.php 4.5 KB

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