DefaultProfile.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. namespace Mrgoon\AliyunSmsSdk\Profile;
  21. use Mrgoon\AliyunSmsSdk\Auth\Credential;
  22. use Mrgoon\AliyunSmsSdk\Auth\ShaHmac1Signer;
  23. use Mrgoon\AliyunSmsSdk\Regions\Endpoint;
  24. use Mrgoon\AliyunSmsSdk\Regions\EndpointProvider;
  25. use Mrgoon\AliyunSmsSdk\Regions\ProductDomain;
  26. class DefaultProfile implements IClientProfile
  27. {
  28. private static $profile;
  29. private static $endpoints;
  30. private static $credential;
  31. private static $regionId;
  32. private static $acceptFormat;
  33. private static $isigner;
  34. private static $iCredential;
  35. private function __construct($regionId,$credential)
  36. {
  37. self::$regionId = $regionId;
  38. self::$credential = $credential;
  39. }
  40. public static function getProfile($regionId, $accessKeyId, $accessSecret)
  41. {
  42. $credential =new Credential($accessKeyId, $accessSecret);
  43. self::$profile = new DefaultProfile($regionId, $credential);
  44. return self::$profile;
  45. }
  46. public function getSigner()
  47. {
  48. if(null == self::$isigner)
  49. {
  50. self::$isigner = new ShaHmac1Signer();
  51. }
  52. return self::$isigner;
  53. }
  54. public function getRegionId()
  55. {
  56. return self::$regionId;
  57. }
  58. public function getFormat()
  59. {
  60. return self::$acceptFormat;
  61. }
  62. public function getCredential()
  63. {
  64. if(null == self::$credential && null != self::$iCredential)
  65. {
  66. self::$credential = self::$iCredential;
  67. }
  68. return self::$credential;
  69. }
  70. public static function getEndpoints()
  71. {
  72. if(null == self::$endpoints)
  73. {
  74. self::$endpoints = EndpointProvider::getEndpoints();
  75. }
  76. return self::$endpoints;
  77. }
  78. public static function addEndpoint($endpointName, $regionId, $product, $domain)
  79. {
  80. if(null == self::$endpoints)
  81. {
  82. self::$endpoints = self::getEndpoints();
  83. }
  84. $endpoint = self::findEndpointByName($endpointName);
  85. if(null == $endpoint)
  86. {
  87. self::addEndpoint_($endpointName, $regionId, $product, $domain);
  88. }
  89. else
  90. {
  91. self::updateEndpoint($regionId, $product, $domain, $endpoint);
  92. }
  93. }
  94. public static function findEndpointByName($endpointName)
  95. {
  96. foreach (self::$endpoints as $key => $endpoint)
  97. {
  98. if($endpoint->getName() == $endpointName)
  99. {
  100. return $endpoint;
  101. }
  102. }
  103. }
  104. private static function addEndpoint_($endpointName,$regionId, $product, $domain)
  105. {
  106. $regionIds = array($regionId);
  107. $productsDomains = array(new ProductDomain($product, $domain));
  108. $endpoint = new Endpoint($endpointName, $regionIds, $productsDomains);
  109. array_push(self::$endpoints, $endpoint);
  110. }
  111. private static function updateEndpoint($regionId, $product, $domain, $endpoint)
  112. {
  113. $regionIds = $endpoint->getRegionIds();
  114. if(!in_array($regionId,$regionIds))
  115. {
  116. array_push($regionIds, $regionId);
  117. $endpoint->setRegionIds($regionIds);
  118. }
  119. $productDomains = $endpoint->getProductDomains();
  120. if(null == self::findProductDomain($productDomains, $product, $domain))
  121. {
  122. array_push($productDomains, new ProductDomain($product, $domain));
  123. }
  124. $endpoint->setProductDomains($productDomains);
  125. }
  126. private static function findProductDomain($productDomains, $product, $domain)
  127. {
  128. foreach ($productDomains as $key => $productDomain)
  129. {
  130. if($productDomain->getProductName() == $product && $productDomain->getDomainName() == $domain)
  131. {
  132. return $productDomain;
  133. }
  134. }
  135. return null;
  136. }
  137. }