api.fga.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Implements fucking great advices interface
  4. */
  5. class FGA {
  6. /**
  7. * System cache object placeholder
  8. *
  9. * @var object
  10. */
  11. protected $cache = '';
  12. /**
  13. * Default response timeout in seconds
  14. *
  15. * @var int
  16. */
  17. protected $responseTimeout = 1;
  18. /**
  19. * Default advices caching timeout in seconds
  20. *
  21. * @var int
  22. */
  23. protected $cacheTimeout = 86400;
  24. /**
  25. * Contains remote advice api placeholder
  26. *
  27. * @var object
  28. */
  29. protected $adviceApi = '';
  30. /**
  31. * Contains current two-letters locale
  32. *
  33. * @var string
  34. */
  35. protected $currentLocale = '';
  36. /**
  37. * Predefined routes/URLs/etc
  38. */
  39. const URL_ADVICE = 'http://ubilling.net.ua/fga/api/random/';
  40. const ROUTE_LANG = '?lang=';
  41. const ADVICE_KEY = 'FGADVICE';
  42. /**
  43. * Creates new awesome advices instance
  44. */
  45. public function __construct() {
  46. $this->initCache();
  47. $this->setLocale();
  48. }
  49. /**
  50. * Sets current instance locale
  51. *
  52. * @return void
  53. */
  54. protected function setLocale() {
  55. $this->currentLocale = curlang();
  56. }
  57. /**
  58. * Inits remote advice API handler
  59. *
  60. * @return void
  61. */
  62. protected function initAdviceApi() {
  63. $apiUrl = self::URL_ADVICE;
  64. if (!empty($this->currentLocale)) {
  65. $apiUrl .= self::ROUTE_LANG . $this->currentLocale;
  66. }
  67. $this->adviceApi = new OmaeUrl($apiUrl);
  68. $this->adviceApi->setTimeout(1);
  69. }
  70. /**
  71. * Inits Ubilling caching engine
  72. *
  73. * @return void
  74. */
  75. protected function initCache() {
  76. $this->cache = new UbillingCache();
  77. }
  78. /**
  79. * Returns a random advice
  80. *
  81. * @return string
  82. */
  83. public function getRandomAdvice() {
  84. $this->initAdviceApi();
  85. $result = '';
  86. $randomAdviceRaw = $this->adviceApi->response();
  87. if (!empty($randomAdviceRaw)) {
  88. $randomAdviceText = json_decode($randomAdviceRaw, true);
  89. if (is_array($randomAdviceText)) {
  90. $result .= @$randomAdviceText['text'];
  91. }
  92. }
  93. //something went wrong?
  94. if (empty($result)) {
  95. $result = 'Oo';
  96. }
  97. return($result);
  98. }
  99. /**
  100. * Returns advice of the day
  101. *
  102. * @return string
  103. */
  104. public function getAdviceOfTheDay() {
  105. $result = '';
  106. $this->initCache();
  107. $cachedData = $this->cache->get(self::ADVICE_KEY, $this->cacheTimeout);
  108. if (empty($cachedData)) {
  109. //getting new advice
  110. $randomAdvice = $this->getRandomAdvice();
  111. $result .= $randomAdvice;
  112. //updating cache
  113. $this->cache->set(self::ADVICE_KEY, $randomAdvice, $this->cacheTimeout);
  114. } else {
  115. $result .= $cachedData;
  116. }
  117. return($result);
  118. }
  119. }