ExampleCacheTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright Zikula Foundation 2014 - Zikula Application Framework
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPv3 (or at your option any later version).
  9. * @package OpenWeatherMap-PHP-Api
  10. *
  11. * Please see the NOTICE file distributed with this source code for further
  12. * information regarding copyright and licensing.
  13. */
  14. namespace Cmfcmf\OpenWeatherMap\Tests\OpenWeatherMap;
  15. use Cmfcmf\OpenWeatherMap\AbstractCache;
  16. require_once __DIR__ . '/bootstrap.php';
  17. /**
  18. * Example cache implementation for doing unit testing.
  19. *
  20. * @ignore
  21. */
  22. class ExampleCacheTest extends AbstractCache
  23. {
  24. protected $seconds;
  25. protected $tmp;
  26. private function urlToPath($url)
  27. {
  28. $tmp = $this->tmp;
  29. $dir = $tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
  30. if (!is_dir($dir)) {
  31. mkdir($dir);
  32. }
  33. $path = $dir . DIRECTORY_SEPARATOR . md5($url);
  34. return $path;
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function isCached($url)
  40. {
  41. $path = $this->urlToPath($url);
  42. if (!file_exists($path) || filectime($path) + $this->seconds < time()) {
  43. return false;
  44. }
  45. return true;
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function getCached($url)
  51. {
  52. return file_get_contents($this->urlToPath($url));
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. public function setCached($url, $content)
  58. {
  59. file_put_contents($this->urlToPath($url), $content);
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function setSeconds($seconds)
  65. {
  66. $this->seconds = $seconds;
  67. }
  68. /**
  69. * @inheritdoc
  70. */
  71. public function setTempPath($path)
  72. {
  73. if (!is_dir($path)) {
  74. mkdir($path);
  75. }
  76. $this->tmp = $path;
  77. }
  78. }