Gateway.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /*
  3. * This file is part of the overtrue/easy-sms.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\EasySms\Gateways;
  11. use Overtrue\EasySms\Contracts\GatewayInterface;
  12. use Overtrue\EasySms\Support\Config;
  13. /**
  14. * Class Gateway.
  15. */
  16. abstract class Gateway implements GatewayInterface
  17. {
  18. const DEFAULT_TIMEOUT = 5.0;
  19. /**
  20. * @var \Overtrue\EasySms\Support\Config
  21. */
  22. protected $config;
  23. /**
  24. * @var array
  25. */
  26. protected $options;
  27. /**
  28. * @var float
  29. */
  30. protected $timeout;
  31. /**
  32. * Gateway constructor.
  33. *
  34. * @param array $config
  35. */
  36. public function __construct(array $config)
  37. {
  38. $this->config = new Config($config);
  39. }
  40. /**
  41. * Return timeout.
  42. *
  43. * @return int|mixed
  44. */
  45. public function getTimeout()
  46. {
  47. return $this->timeout ?: $this->config->get('timeout', self::DEFAULT_TIMEOUT);
  48. }
  49. /**
  50. * Set timeout.
  51. *
  52. * @param int $timeout
  53. *
  54. * @return $this
  55. */
  56. public function setTimeout($timeout)
  57. {
  58. $this->timeout = floatval($timeout);
  59. return $this;
  60. }
  61. /**
  62. * @return \Overtrue\EasySms\Support\Config
  63. */
  64. public function getConfig()
  65. {
  66. return $this->config;
  67. }
  68. /**
  69. * @param \Overtrue\EasySms\Support\Config $config
  70. *
  71. * @return $this
  72. */
  73. public function setConfig(Config $config)
  74. {
  75. $this->config = $config;
  76. return $this;
  77. }
  78. /**
  79. * @param $options
  80. *
  81. * @return $this
  82. */
  83. public function setGuzzleOptions($options)
  84. {
  85. $this->options = $options;
  86. return $this;
  87. }
  88. /**
  89. * @return array
  90. */
  91. public function getGuzzleOptions()
  92. {
  93. return $this->options ?: $this->config->get('options', []);
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function getName()
  99. {
  100. return \strtolower(str_replace([__NAMESPACE__.'\\', 'Gateway'], '', \get_class($this)));
  101. }
  102. }