VerifyTicket.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  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. /**
  11. * VerifyTicket.php.
  12. *
  13. * Part of Overtrue\WeChat.
  14. *
  15. * For the full copyright and license information, please view the LICENSE
  16. * file that was distributed with this source code.
  17. *
  18. * @author mingyoung <mingyoungcheung@gmail.com>
  19. * @author lixiao <leonlx126@gmail.com>
  20. * @copyright 2016
  21. *
  22. * @see https://github.com/overtrue
  23. * @see http://overtrue.me
  24. */
  25. namespace EasyWeChat\OpenPlatform;
  26. use Doctrine\Common\Cache\Cache;
  27. use EasyWeChat\Core\Exceptions\RuntimeException;
  28. class VerifyTicket
  29. {
  30. /**
  31. * Cache manager.
  32. *
  33. * @var \Doctrine\Common\Cache\Cache
  34. */
  35. protected $cache;
  36. /**
  37. * App Id.
  38. *
  39. * @var string
  40. */
  41. protected $appId;
  42. /**
  43. * Cache Key.
  44. *
  45. * @var string
  46. */
  47. private $cacheKey;
  48. /**
  49. * Cache key prefix.
  50. *
  51. * @var string
  52. */
  53. protected $prefix = 'easywechat.open_platform.component_verify_ticket.';
  54. /**
  55. * VerifyTicket constructor.
  56. *
  57. * @param string $appId
  58. * @param \Doctrine\Common\Cache\Cache $cache
  59. */
  60. public function __construct($appId, Cache $cache)
  61. {
  62. $this->appId = $appId;
  63. $this->cache = $cache;
  64. }
  65. /**
  66. * Set component verify ticket to the cache.
  67. *
  68. * @param string $ticket
  69. *
  70. * @return bool
  71. */
  72. public function setTicket($ticket)
  73. {
  74. return $this->cache->save($this->getCacheKey(), $ticket);
  75. }
  76. /**
  77. * Get component verify ticket.
  78. *
  79. * @return string
  80. *
  81. * @throws \EasyWeChat\Core\Exceptions\RuntimeException
  82. */
  83. public function getTicket()
  84. {
  85. if ($cached = $this->cache->fetch($this->getCacheKey())) {
  86. return $cached;
  87. }
  88. throw new RuntimeException('Component verify ticket does not exists.');
  89. }
  90. /**
  91. * Set component verify ticket cache key.
  92. *
  93. * @param string $cacheKey
  94. *
  95. * @return $this
  96. */
  97. public function setCacheKey($cacheKey)
  98. {
  99. $this->cacheKey = $cacheKey;
  100. return $this;
  101. }
  102. /**
  103. * Get component verify ticket cache key.
  104. *
  105. * @return string $this->cacheKey
  106. */
  107. public function getCacheKey()
  108. {
  109. if (is_null($this->cacheKey)) {
  110. return $this->prefix.$this->appId;
  111. }
  112. return $this->cacheKey;
  113. }
  114. }